diff options
| author | mo khan <mo@mokhan.ca> | 2014-02-21 22:57:51 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2014-02-21 22:57:51 -0700 |
| commit | 33cb4e479decb1f7e15048b8c81924fe7498aec1 (patch) | |
| tree | 0ce2c2e506e95fa2d1146e9b5505f0d718529bae | |
| parent | bf5c3435155c0ee9c2172bea6b6b3c5dec2c2463 (diff) | |
create users table and save applicant with the license.
| -rw-r--r-- | app/models/license.rb | 1 | ||||
| -rw-r--r-- | app/models/user.rb | 10 | ||||
| -rw-r--r-- | db/migrate/20140222054248_create_users.rb | 8 | ||||
| -rw-r--r-- | db/migrate/20140222055630_add_applicant_to_licenses.rb | 5 | ||||
| -rw-r--r-- | db/schema.rb | 9 | ||||
| -rw-r--r-- | spec/models/license_spec.rb | 16 | ||||
| -rw-r--r-- | spec/models/user_spec.rb | 19 |
7 files changed, 44 insertions, 24 deletions
diff --git a/app/models/license.rb b/app/models/license.rb index ccf9b23..6170683 100644 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -2,6 +2,7 @@ class License < ActiveRecord::Base belongs_to :company belongs_to :well_type has_one :location + belongs_to :applicant, class_name: 'User', foreign_key: 'user_id' def self.most_recent(page: 1, per_page: 10) offset = (page - 1) * per_page diff --git a/app/models/user.rb b/app/models/user.rb index f2197dd..446d3e2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,16 +1,12 @@ -class User - attr_reader :company - - def initialize(company: nil) - @company = company - end +class User < ActiveRecord::Base + belongs_to :company def apply_for(well_type, location) license = License.new license.company = company license.well_type = well_type license.location = location - #license.applicant = self + license.applicant = self license.save! license end diff --git a/db/migrate/20140222054248_create_users.rb b/db/migrate/20140222054248_create_users.rb new file mode 100644 index 0000000..791bf2f --- /dev/null +++ b/db/migrate/20140222054248_create_users.rb @@ -0,0 +1,8 @@ +class CreateUsers < ActiveRecord::Migration + def change + create_table :users, id: :uuid do |t| + t.uuid :company_id + t.timestamps + end + end +end diff --git a/db/migrate/20140222055630_add_applicant_to_licenses.rb b/db/migrate/20140222055630_add_applicant_to_licenses.rb new file mode 100644 index 0000000..ca96b8f --- /dev/null +++ b/db/migrate/20140222055630_add_applicant_to_licenses.rb @@ -0,0 +1,5 @@ +class AddApplicantToLicenses < ActiveRecord::Migration + def change + add_column :licenses, :user_id, :uuid + end +end diff --git a/db/schema.rb b/db/schema.rb index c9d8190..d58c3ae 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140222053352) do +ActiveRecord::Schema.define(version: 20140222055630) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -31,6 +31,7 @@ ActiveRecord::Schema.define(version: 20140222053352) do t.uuid "company_id" t.boolean "confidential", default: false t.integer "well_type_id" + t.uuid "user_id" end create_table "locations", id: :uuid, default: "uuid_generate_v4()", force: true do |t| @@ -42,6 +43,12 @@ ActiveRecord::Schema.define(version: 20140222053352) do t.datetime "updated_at" end + create_table "users", id: :uuid, default: "uuid_generate_v4()", force: true do |t| + t.uuid "company_id" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "well_types", force: true do |t| t.string "name" t.string "acronym" diff --git a/spec/models/license_spec.rb b/spec/models/license_spec.rb index 8fbf01a..a939372 100644 --- a/spec/models/license_spec.rb +++ b/spec/models/license_spec.rb @@ -29,20 +29,4 @@ describe License do results.first.should == newest_license end end - - describe "#apply_for" do - context "when applying for a license" do - it "creates a new license" do - company = Company.create(name: 'ABC Resources Ltd.') - user = User.new(company: company) - location = Location.new(latitude: 51.06, longitude: -114.09, township: '1') - license = user.apply_for(WellType::NFW, location) - - license.company.should == user.company - license.well_type.should == WellType::NFW - license.location.should == location - #license.applicant.should == user - end - end - end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 0000000..02e52be --- /dev/null +++ b/spec/models/user_spec.rb @@ -0,0 +1,19 @@ +require "spec_helper" + +describe User do + describe "#apply_for" do + context "when applying for a license" do + it "creates a new license" do + company = Company.create(name: 'ABC Resources Ltd.') + user = User.create(company: company) + location = Location.new(latitude: 51.06, longitude: -114.09, township: '1') + license = user.apply_for(WellType::NFW, location) + + license.company.should == user.company + license.well_type.should == WellType::NFW + license.location.should == location + license.applicant.should == user + end + end + end +end |
