Skip to content

Commit

Permalink
Override _read_attribute in Dirty plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Nov 29, 2017
1 parent 6d00774 commit 9b55ccf
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/mobility/plugins/active_record/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def initialize(*attribute_names)
define_method :previous_changes do
(@previously_changed ||= ActiveSupport::HashWithIndifferentAccess.new).merge(super())
end

define_read_attribute
end

# Overrides +ActiveRecord::AttributeMethods::ClassMethods#has_attribute+ to treat fallthrough attribute methods
Expand All @@ -52,6 +54,22 @@ def included(model_class)
end
model_class.extend has_attribute
end

private

def define_read_attribute
names = @attribute_names
method_name_regex = /\A(#{names.join('|'.freeze)})_([a-z]{2}(_[a-z]{2})?)(=?|\??)\z/.freeze
define_method :_read_attribute do |attr, *args|
if @attributes.key?(attr)
super(attr, *args)
elsif names.include?(attr) || method_name_regex.match?(attr)
__send__(attr)
else
super(attr, *args)
end
end
end
end
end
end
Expand Down

0 comments on commit 9b55ccf

Please sign in to comment.