Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use values returned by attribute setter methods #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/enumerated_attribute/integrations/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class << self
def new(*args, &block)
result = new_without_enumerated_attribute(*args, &block)
params = (!args.empty? && args.first.instance_of?(Hash)) ? args.first : {}
params.each { |k, v| result.write_enumerated_attribute(k, v) }
params.each { |k, v| result.write_enumerated_attribute(k, result[k.to_s]) if self.has_enumerated_attribute?(k.to_s) }
result.initialize_enumerated_attributes(true)
yield result if block_given?
result
Expand Down
14 changes: 12 additions & 2 deletions spec/active_record/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,16 @@
s = RaceCar.find r.id
s.lights.should == "--- :off\n"
s[:lights].should == "--- :off\n"
end

end

it "should use values returned by attribute setter methods" do
r =RaceCar.new(:license_plate_number => ' asdf ')
r.license_plate_number.should == 'asdf'
end

it "should not generate Active Record attributes on #new for keys that are not really active record attributes" do
r =RaceCar.new(:non_active_record_attribute => 'some value')
r.should_not have_attribute(:non_active_record_attribute)
end

end
7 changes: 7 additions & 0 deletions spec/active_record/race_car.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
class RaceCar < ActiveRecord::Base
enum_attr :gear, %w(reverse ^neutral first second over_drive)
enum_attr :choke, %w(^none medium full)

attr_accessor :non_active_record_attribute

def license_plate_number=(value)
value = value.strip # remove whitespace from start and end of value
write_attribute(:license_plate_number, value)
end
end

#gear = enumerated column attribute
Expand Down
1 change: 1 addition & 0 deletions spec/active_record/test_in_memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
connection = ActiveRecord::Base.connection
connection.create_table(:race_cars, :force=>true) do |t|
t.string :name
t.string :license_plate_number
t.enum :gear
t.enum :lights
t.timestamps
Expand Down