Skip to content

Commit

Permalink
Merge pull request #21 from irmela/fix-deprecation-warnings-rails-5-1
Browse files Browse the repository at this point in the history
Fix deprecation warnings rails 5 1
  • Loading branch information
kraatob committed Jan 23, 2018
2 parents f07e99b + 20272b3 commit 53a3c19
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Expand Up @@ -4,6 +4,7 @@ rvm:
- 1.8.7
- 2.1.8
- 2.3.1
- 2.4.2

gemfile:
- gemfiles/Gemfile.2.3
Expand All @@ -18,10 +19,16 @@ matrix:
rvm: 2.1.8
- gemfile: gemfiles/Gemfile.2.3
rvm: 2.3.1
- gemfile: gemfiles/Gemfile.2.3
rvm: 2.4.2
- gemfile: gemfiles/Gemfile.3.2
rvm: 2.3.1
- gemfile: gemfiles/Gemfile.3.2
rvm: 2.4.2
- gemfile: gemfiles/Gemfile.4.2
rvm: 1.8.7
- gemfile: gemfiles/Gemfile.4.2
rvm: 2.4.2
- gemfile: gemfiles/Gemfile.5.0
rvm: 2.1.8
- gemfile: gemfiles/Gemfile.5.0
Expand Down
Expand Up @@ -27,11 +27,15 @@ def association_id(record)
end

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

def previously_saved_value(record)
if old_id = record.send(association_id_was_method).presence
if old_id = association_id_was(record)
if old_id == association_id(record)
current_value(record) # no need to query the database if nothing changed
else
Expand All @@ -46,6 +50,14 @@ def current_value(record)

private

def association_id_was(record)
if record.respond_to?(:attribute_in_database)
record.attribute_in_database(:"#{association_id_method}").presence # Rails >= 5.1
else
record.send(association_id_was_method).presence # Rails <= 5.0
end
end

def association_id_was_method
:"#{association_id_method}_was"
end
Expand Down
Expand Up @@ -75,15 +75,27 @@ def decorate_values(values)
end

def has_previously_saved_value?(record)
!record.new_record? && record.respond_to?(value_was_method)
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
end
end

def previously_saved_value(record)
record.send(value_was_method)
value_was(record)
end

private

def value_was(record)
if record.respond_to?(:attribute_in_database)
record.attribute_in_database(:"#{property}") # Rails >= 5.1
else
record.send(value_was_method) # Rails <= 5.0
end
end

def value_was_method
:"#{property}_was"
end
Expand Down

0 comments on commit 53a3c19

Please sign in to comment.