require File.expand_path(File.dirname(__FILE__) + '/spec_helper') require File.expand_path(File.dirname(__FILE__) + '/../init.rb') require File.expand_path(File.dirname(__FILE__) + '/lib/activerecord_test_case') describe "ColumnWithSpaces" do it "should not be included by the plugin's init hook" do Employee.included_modules.should_not include(DentzSinclair::ColumnWithSpaces) end it "should be able to be included into ActiveRecord models" do LegacyEmployee.included_modules.should include(DentzSinclair::ColumnWithSpaces) end end describe ".find_by on model for legacy db table that has spaces in column labels with ColumnWithSpaces module mixed in" do before(:all) do Fixtures.create_fixtures(fixture_path, ActiveRecord::Base.connection.tables) end it "should create find_by substituting spaces for underscores in method call" do LegacyEmployee.find_by_first_name("One").should be_kind_of(LegacyEmployee) end it "should not substitute spaces for underscores if column label has underscores" do LegacyEmployee.find_by_middle_name("Five").should be_kind_of(LegacyEmployee) end it "should create a valid find_by method when mixing column labels that have spaces and underscores" do LegacyEmployee.find_by_first_name_and_middle_name("One","Five").should be_kind_of(LegacyEmployee) end end describe "Model for legacy database that has column names with spaces without ColumnWithSpaces module mixed in" do before do @employee = Employee.create end it "should raise error calling a column attribute substituting spaces with underscores" do @employee.attributes = valid_employee_attributes lambda { @employee.first_name }.should raise_error(NoMethodError) end it "should return value using send method to call the column/attribute name with spaces" do @employee.attributes = valid_employee_attributes @employee.send('first name').should eql('Carl') end it "should return value for column/attribute names with underscores in them" do @employee.attributes = valid_employee_attributes @employee.middle_name end def valid_employee_attributes { 'first name' => 'Carl', 'last name' => 'Right', :middle_name => 'Robert' } end end describe "Model for legacy database that has column names with spaces with ColumnWithSpaces module mixed in" do before do @legacy = LegacyEmployee.create end it "should return value for column name with spaces when called by substituting underscores for spaces" do @legacy.attributes = valid_legacy_attributes @legacy.first_name.should eql('Red') end it "should assign value to column name with spaces by substituting underscores for spaces" do @legacy.first_name = 'Walter' @legacy.first_name.should eql('Walter') end it "should return value for column name with spaces via send method" do @legacy.attributes = valid_legacy_attributes @legacy.send('first name').should eql('Red') end it "should assign value for column name with spaces via send method" do @legacy.send('first name=','Walter') @legacy.first_name.should eql('Walter') end it "should assign and read value for column names with underscores in them" do @legacy.middle_name = 'Thomas' @legacy.middle_name.should eql('Thomas') end it "should be overridden by a real column with an underscore when reading the attribute" do @legacy.attributes = valid_legacy_attributes @legacy.last_name.should eql('Black') @legacy.send('last name').should eql('Blue') end it "should be overridden by a real column with an underscore when writing" do @legacy.last_name = 'Walters' @legacy.send('last name=', 'Thomas') @legacy.last_name.should eql('Walters') @legacy.send('last name').should eql('Thomas') end def valid_legacy_attributes { 'first name' => 'Red', 'last name' => 'Blue', :last_name => 'Black', :middle_name => 'White' } end end