diff --git a/lib/lol_dba.rb b/lib/lol_dba.rb index e343cbb..1e1fcb1 100644 --- a/lib/lol_dba.rb +++ b/lib/lol_dba.rb @@ -48,7 +48,7 @@ def self.validate_and_sort_indexes(indexes_required) keys_to_add = foreign_keys.uniq - existing_indexes missing_indexes[table_name] = keys_to_add unless keys_to_add.empty? else - warning_messages << "BUG: table '#{table_name.to_s}' does not exist, please report this bug.\n " + warning_messages << "BUG: table '#{table_name.to_s}' does not exist, please report this bug.\n" end rescue Exception => e puts "ERROR: #{e}" @@ -112,8 +112,13 @@ def self.check_for_indexes(migration_format = false) index_name = foreign_key.to_s end when :has_and_belongs_to_many - table_name = reflection_options.options[:join_table] - table_name ||= [class_name.table_name, reflection_name.to_s].sort.join('_') + # in older rails versions, join_table is not present + if reflection_options.respond_to?(:join_table) + table_name = reflection_options.join_table + else + table_name ||= reflection_options.options[:join_table] + table_name ||= [class_name.table_name, reflection_options.table_name].sort.join('_') + end association_foreign_key = reflection_options.options[:association_foreign_key] ||= "#{reflection_name.to_s.singularize}_id" foreign_key = get_through_foreign_key(class_name, reflection_options) diff --git a/spec/associations_index_spec.rb b/spec/associations_index_spec.rb index 28d2ac7..aef9381 100644 --- a/spec/associations_index_spec.rb +++ b/spec/associations_index_spec.rb @@ -5,7 +5,7 @@ before :all do lol_dba = LolDba.check_for_indexes @relationship_indexes = lol_dba[0] - @warning_messages = lol_dba[1] + @warning_messages = lol_dba[1].split("\n") end it "find relationship indexes" do @@ -62,9 +62,11 @@ end it "have warnings(non-existent table) on test data" do - expect(@warning_messages).not_to be_empty - expect(@warning_messages).to match(/\'wrongs\'/) - expect(@warning_messages).to match(/\'addresses_wrongs\'/) + expected_warnings = [ + "BUG: table 'wrongs' does not exist, please report this bug.", + "BUG: table 'addresses_wrongs' does not exist, please report this bug." + ] + expect(@warning_messages).to match_array(expected_warnings) end it "find indexes for STI" do @@ -75,6 +77,12 @@ expect(@relationship_indexes["freelancers"]).to include(["id", "worker_type"]) end + it "finds the right join table for HABTM for an STI subclass" do + expect(@relationship_indexes).not_to have_key('companies_worker_users') + expect(@relationship_indexes).to have_key('companies_users') + expect(@relationship_indexes["companies_users"]).to include(["company_id", "user_id"]) + end + it "find indexes, than use custom class name option in association" do expect(@relationship_indexes["employers_freelancers"]).to be_nil expect(@relationship_indexes["companies_freelancers"]).to include(["company_id", "freelancer_id"]) diff --git a/spec/fixtures/app/models/company.rb b/spec/fixtures/app/models/company.rb index 661592f..b4af1ea 100644 --- a/spec/fixtures/app/models/company.rb +++ b/spec/fixtures/app/models/company.rb @@ -8,4 +8,5 @@ class Company < ActiveRecord::Base has_many :users has_and_belongs_to_many :freelancers + has_and_belongs_to_many :worker_users, join_table: 'companies_users', association_foreign_key: 'user_id' end diff --git a/spec/fixtures/schema.rb b/spec/fixtures/schema.rb index 16cb729..a05eec2 100644 --- a/spec/fixtures/schema.rb +++ b/spec/fixtures/schema.rb @@ -53,6 +53,11 @@ t.column "company_id", :integer end + create_table "companies_users", :id => false, :force => true do |t| + t.column "company_id", :integer + t.column "user_id", :integer + end + create_table "gifts", :primary_key => "custom_primary_key", :force => true do |t| t.column "name", :string t.column "price", :integer