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

[D-326] Store accessor support for assignable_values #50

Merged
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 .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-20.04
services:
mysql:
image: mysql:5.6
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: password
ports:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html

-

## 1.1.0 - 2024-12-05

### Compatible changes

- Add complete support for attributes that are available through store accessors

## 1.0.1 - 2024-08-27

- Hook into railties correctly
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.5.0.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
assignable_values (1.0.1)
assignable_values (1.1.0)
activerecord (>= 2.3)

GEM
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.5.1.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
assignable_values (1.0.1)
assignable_values (1.1.0)
activerecord (>= 2.3)

GEM
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.5.1.pg.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
assignable_values (1.0.1)
assignable_values (1.1.0)
activerecord (>= 2.3)

GEM
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.6.1.pg.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
assignable_values (1.0.1)
assignable_values (1.1.0)
activerecord (>= 2.3)

GEM
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.7.1.pg.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
assignable_values (1.0.1)
assignable_values (1.1.0)
activerecord (>= 2.3)

GEM
Expand Down
1 change: 1 addition & 0 deletions lib/assignable_values.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
require 'assignable_values/active_record/restriction/base'
require 'assignable_values/active_record/restriction/belongs_to_association'
require 'assignable_values/active_record/restriction/scalar_attribute'
require 'assignable_values/active_record/restriction/store_accessor_attribute'
require 'assignable_values/humanized_value'
require 'assignable_values/humanizable_string'
17 changes: 16 additions & 1 deletion lib/assignable_values/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ module ActiveRecord
private

def assignable_values_for(property, options = {}, &values)
restriction_type = belongs_to_association?(property) ? Restriction::BelongsToAssociation : Restriction::ScalarAttribute
restriction_type = if belongs_to_association?(property)
Restriction::BelongsToAssociation
elsif store_accessor_attribute?(property)
Restriction::StoreAccessorAttribute
else
Restriction::ScalarAttribute
end

restriction_type.new(self, property, options, &values)
end

Expand All @@ -13,6 +20,14 @@ def belongs_to_association?(property)
reflection && reflection.macro == :belongs_to
end

def store_accessor_attribute?(property)
store_identifier_of(property).present?
end

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

end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ def decorate_values(values, klass)
end

def has_previously_saved_value?(record)
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
if record.respond_to?(:attribute_in_database) # Rails >= 5.1
!record.new_record?
else # Rails <= 5.0
!record.new_record? && record.respond_to?(value_was_method)
end
end

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

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
if record.respond_to?(:attribute_in_database) # Rails >= 5.1
record.attribute_in_database(:"#{property}")
else # Rails <= 5.0
record.send(value_was_method)
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module AssignableValues
module ActiveRecord
module Restriction
class StoreAccessorAttribute < ScalarAttribute

private

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

def value_was_method
:"#{store_identifier}_was"
end

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

accessor.with_indifferent_access[property]
end

end
end
end
end
2 changes: 1 addition & 1 deletion lib/assignable_values/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module AssignableValues
VERSION = '1.0.1'
VERSION = '1.1.0'
end
Loading