From 4f5679aac38327eff7af0741f2265c1e0122ce20 Mon Sep 17 00:00:00 2001 From: Andrew Spinks Date: Wed, 31 Jul 2013 16:00:28 +1000 Subject: [PATCH] Add EnterpriseRole model and link up to users and enterprises. --- app/models/enterprise.rb | 2 ++ app/models/enterprise_role.rb | 4 ++++ app/models/spree/user_decorator.rb | 4 ++++ .../20130729030515_add_enterprise_role.rb | 8 ++++++++ db/schema.rb | 18 ++++++++++-------- lib/tasks/dev.rake | 1 + spec/support/spree/init.rb | 2 -- 7 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 app/models/enterprise_role.rb create mode 100644 app/models/spree/user_decorator.rb create mode 100644 db/migrate/20130729030515_add_enterprise_role.rb diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 868ec7be52..16a35609f8 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -4,6 +4,8 @@ class Enterprise < ActiveRecord::Base belongs_to :address, :class_name => 'Spree::Address' has_many :product_distributions, :foreign_key => 'distributor_id', :dependent => :destroy has_many :distributed_products, :through => :product_distributions, :source => :product + has_many :enterprise_roles + has_many :users, through: :enterprise_roles accepts_nested_attributes_for :address diff --git a/app/models/enterprise_role.rb b/app/models/enterprise_role.rb new file mode 100644 index 0000000000..94b926f84f --- /dev/null +++ b/app/models/enterprise_role.rb @@ -0,0 +1,4 @@ +class EnterpriseRole < ActiveRecord::Base + belongs_to :user, :class_name => Spree.user_class + belongs_to :enterprise +end diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb new file mode 100644 index 0000000000..58870b787b --- /dev/null +++ b/app/models/spree/user_decorator.rb @@ -0,0 +1,4 @@ +Spree.user_class.class_eval do + has_many :enterprise_roles, :dependent => :destroy + has_many :enterprises, through: :enterprise_roles +end diff --git a/db/migrate/20130729030515_add_enterprise_role.rb b/db/migrate/20130729030515_add_enterprise_role.rb new file mode 100644 index 0000000000..25a1deeeac --- /dev/null +++ b/db/migrate/20130729030515_add_enterprise_role.rb @@ -0,0 +1,8 @@ +class AddEnterpriseRole < ActiveRecord::Migration + def change + create_table :enterprise_roles do |t| + t.references :user, index: true + t.references :enterprise, index: true + end + end +end diff --git a/db/schema.rb b/db/schema.rb index d7c132b227..eb4022ce3f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130729021924) do +ActiveRecord::Schema.define(:version => 20130729030515) do create_table "cms_blocks", :force => true do |t| t.integer "page_id", :null => false @@ -130,11 +130,6 @@ ActiveRecord::Schema.define(:version => 20130729021924) do add_index "cms_snippets", ["site_id", "identifier"], :name => "index_cms_snippets_on_site_id_and_identifier", :unique => true add_index "cms_snippets", ["site_id", "position"], :name => "index_cms_snippets_on_site_id_and_position" - create_table "coordinator_fees", :id => false, :force => true do |t| - t.integer "order_cycle_id" - t.integer "enterprise_fee_id" - end - create_table "enterprise_fees", :force => true do |t| t.integer "enterprise_id" t.string "fee_type" @@ -143,6 +138,11 @@ ActiveRecord::Schema.define(:version => 20130729021924) do t.datetime "updated_at", :null => false end + create_table "enterprise_roles", :force => true do |t| + t.integer "user_id" + t.integer "enterprise_id" + end + create_table "enterprises", :force => true do |t| t.string "name" t.string "description" @@ -194,8 +194,10 @@ ActiveRecord::Schema.define(:version => 20130729021924) do t.datetime "orders_open_at" t.datetime "orders_close_at" t.integer "coordinator_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.integer "coordinator_admin_fee_id" + t.integer "coordinator_sales_fee_id" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "product_distributions", :force => true do |t| diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index abbf1214de..576fe8fca9 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -6,6 +6,7 @@ namespace :openfoodweb do desc 'load sample data' task :load_sample_data => :environment do require File.expand_path('../../../spec/factories', __FILE__) + require File.expand_path('../../../spec/support/spree/init', __FILE__) # -- Shipping / payment information unless Spree::Zone.find_by_name 'Australia' diff --git a/spec/support/spree/init.rb b/spec/support/spree/init.rb index 99a4dafdc4..471b7cf2c9 100644 --- a/spec/support/spree/init.rb +++ b/spec/support/spree/init.rb @@ -1,5 +1,3 @@ -require 'factory_girl_rails' - # Initialise shipping method when created without one, like this: # create(:product, :distributors => [...]) # In this case, we don't care what the shipping method is, but we need one for validations to pass.