Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: makandra/assignable_values
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 84359916a3e22bac48a8bbf78d5e449a13f119e7
Choose a base ref
..
head repository: makandra/assignable_values
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: bbbd4f5a4237fcb1694d74019bec9b103a08c416
Choose a head ref
Showing with 47 additions and 19 deletions.
  1. +16 −6 lib/assignable_values/active_record/restriction/scalar_attribute.rb
  2. +31 −13 spec/assignable_values/active_record_spec.rb
Original file line number Diff line number Diff line change
@@ -107,10 +107,10 @@ def decorate_values(values, klass)
end

def has_previously_saved_value?(record)
if !store_accessor_attribute? && record.respond_to?(:attribute_in_database)
if record.respond_to?(:attribute_in_database)
!record.new_record? # Rails >= 5.1
else
!record.new_record? && record.respond_to?(value_was_method) # Rails <= 5.0 and store_accessor
!record.new_record? && record.respond_to?(value_was_method) # Rails <= 5.0
end
end

@@ -119,10 +119,16 @@ def previously_saved_value(record)
end

def value_was(record)
if !store_accessor_attribute? && record.respond_to?(:attribute_in_database)
record.attribute_in_database(:"#{property}") # Rails >= 5.1
if record.respond_to?(:attribute_in_database)
# Rails >= 5.1
if store_accessor_attribute?
accessor = record.attribute_in_database(:"#{store_identifier}").with_indifferent_access
accessor[property]
else
record.attribute_in_database(:"#{property}")
end
else
record.send(value_was_method) # Rails <= 5.0 and store_accessor
record.send(value_was_method) # Rails <= 5.0
end
end

@@ -131,7 +137,11 @@ def value_was_method
end

def store_accessor_attribute?
@model.stored_attributes.any? { |_, attrs| attrs.include?(@property.to_sym) }
store_identifier.present?
end

def store_identifier
@model.stored_attributes.find { |_, attrs| attrs.include?(property.to_sym) }&.first
end

end
44 changes: 31 additions & 13 deletions spec/assignable_values/active_record_spec.rb
Original file line number Diff line number Diff line change
@@ -376,14 +376,10 @@ def save_without_validation(record)

before :each do
@klass = Song.disposable_copy do
store_accessor :metadata, :format, :instruments
store_accessor :metadata, :format

assignable_values_for :format do
%[mp3 wav]
end

assignable_values_for :instruments, multiple: true do
%w[piano drums guitar]
%w[mp3 wav]
end
end
end
@@ -408,13 +404,15 @@ def save_without_validation(record)

it 'should allow a previously saved value even if that value is no longer allowed' do
record = @klass.create!(:format => 'mp3')
@klass.update_all(:format => 'pretend previously valid value') # update without validations for the sake of this test

@klass.update_all(metadata: { 'format' => 'pretend previously valid value' }) # update without validations for the sake of this test
record.reload.should be_valid
end

it 'should allow a previously saved, blank format value even if that value is no longer allowed' do
record = @klass.create!(:format => 'mp3')
@klass.update_all(:format => '') # update without validations for the sake of this test

@klass.update_all(metadata: { 'format' => nil }) # update without validations for the sake of this test
record.reload.should be_valid
end

@@ -439,26 +437,36 @@ def save_without_validation(record)

it 'should generate an instance method to retrieve the humanization of any given value' do
song = @klass.new(:format => 'mp3')
song.humanized_genre('wav').should == 'WAV-Codec'
song.humanized_format('wav').should == 'WAV-Codec'
end

it 'should generate a class method to retrieve the humanization of any given value' do
@klass.humanized_genre('wav').should == 'WAV-Codec'
@klass.humanized_format('wav').should == 'WAV-Codec'
end

context 'for multiple: true' do
before :each do
@klass = Song.disposable_copy do
store_accessor :metadata, :instruments

assignable_values_for :instruments, multiple: true do
%w[piano drums guitar]
end
end
end

it 'should raise when trying to humanize a value without an argument' do
song = @klass.new
proc { song.humanized_instruments }.should raise_error(ArgumentError)
proc { song.humanized_instrument }.should raise_error(ArgumentError)
end

it 'should generate an instance method to retrieve the humanization of any given value' do
song = @klass.new(:instruments => 'drums')
song.humanized_instruments('piano').should == 'Piano'
song.humanized_instrument('piano').should == 'Piano'
end

it 'should generate a class method to retrieve the humanization of any given value' do
@klass.humanized_instruments('piano').should == 'Piano'
@klass.humanized_instrument('piano').should == 'Piano'
end

it 'should generate an instance method to retrieve the humanizations of all current values' do
@@ -477,6 +485,9 @@ def save_without_validation(record)

before :each do
@klass = Song.disposable_copy do

store_accessor :metadata, :format

assignable_values_for :format, :allow_blank => true do
%w[mp3 wav]
end
@@ -498,6 +509,8 @@ def save_without_validation(record)
before :each do
@klass = Song.disposable_copy do

store_accessor :metadata, :format

attr_accessor :format_may_be_blank

assignable_values_for :format, :allow_blank => :format_may_be_blank do
@@ -519,6 +532,8 @@ def save_without_validation(record)
before :each do
@klass = Song.disposable_copy do

store_accessor :metadata, :format

attr_accessor :format_may_be_blank

assignable_values_for :format, :allow_blank => lambda { format_may_be_blank } do
@@ -539,6 +554,9 @@ def save_without_validation(record)

before :each do
@klass = Song.disposable_copy do

store_accessor :metadata, :format

assignable_values_for :format, :message => 'should be something different' do
%w[mp3 wav]
end