Class: PaperTrail::RelatedChanges::Serializer::BelongsTo
- Inherits:
-
Object
- Object
- PaperTrail::RelatedChanges::Serializer::BelongsTo
- Defined in:
- lib/paper_trail/related_changes/serializer/belongs_to.rb
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#change ⇒ Object
readonly
Returns the value of attribute change.
-
#item_type ⇒ Object
readonly
Returns the value of attribute item_type.
Class Method Summary collapse
Instance Method Summary collapse
- #association ⇒ Object
- #attribute_name ⇒ Object
- #find_associated_version(id) ⇒ Object
- #find_current_record(id) ⇒ Object
- #find_record(id) ⇒ Object
-
#initialize(attribute, change) ⇒ BelongsTo
constructor
A new instance of BelongsTo.
- #model_class ⇒ Object
- #serialize ⇒ Object
Constructor Details
#initialize(attribute, change) ⇒ BelongsTo
Returns a new instance of BelongsTo.
22 23 24 25 26 |
# File 'lib/paper_trail/related_changes/serializer/belongs_to.rb', line 22 def initialize(attribute, change) @attribute = attribute @item_type = attribute.version.item_type @change = change end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
18 19 20 |
# File 'lib/paper_trail/related_changes/serializer/belongs_to.rb', line 18 def attribute @attribute end |
#change ⇒ Object (readonly)
Returns the value of attribute change.
18 19 20 |
# File 'lib/paper_trail/related_changes/serializer/belongs_to.rb', line 18 def change @change end |
#item_type ⇒ Object (readonly)
Returns the value of attribute item_type.
18 19 20 |
# File 'lib/paper_trail/related_changes/serializer/belongs_to.rb', line 18 def item_type @item_type end |
Class Method Details
.match(attribute) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/paper_trail/related_changes/serializer/belongs_to.rb', line 4 def self.match(attribute) attribute.version .model_class .reflections .values .select(&:belongs_to?) .reject(&:polymorphic?) .any? { |r| r.join_foreign_key.to_sym == attribute.to_sym } end |
.serialize(attribute, change) ⇒ Object
14 15 16 |
# File 'lib/paper_trail/related_changes/serializer/belongs_to.rb', line 14 def self.serialize(attribute, change) new(attribute, change).serialize end |
Instance Method Details
#association ⇒ Object
44 45 46 47 48 49 |
# File 'lib/paper_trail/related_changes/serializer/belongs_to.rb', line 44 def association @association ||= item_type .constantize .reflections .detect { |_, a| a.foreign_key.to_sym == attribute.to_sym }&.fetch(1) end |
#attribute_name ⇒ Object
40 41 42 |
# File 'lib/paper_trail/related_changes/serializer/belongs_to.rb', line 40 def attribute_name association.name end |
#find_associated_version(id) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/paper_trail/related_changes/serializer/belongs_to.rb', line 64 def find_associated_version(id) PaperTrail::Version.where( PaperTrail::Version.arel_table[:created_at].lt(attribute.version.created_at) ).where( item_id: id, item_type: model_class.name ).order(created_at: :desc).first end |
#find_current_record(id) ⇒ Object
60 61 62 |
# File 'lib/paper_trail/related_changes/serializer/belongs_to.rb', line 60 def find_current_record(id) model_class.find_by(id: id) end |
#find_record(id) ⇒ Object
51 52 53 54 |
# File 'lib/paper_trail/related_changes/serializer/belongs_to.rb', line 51 def find_record(id) return unless id find_associated_version(id).try(:name) || find_current_record(id).try(:name) || "Record no longer exists" end |
#model_class ⇒ Object
56 57 58 |
# File 'lib/paper_trail/related_changes/serializer/belongs_to.rb', line 56 def model_class @model_class ||= association.klass end |
#serialize ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/paper_trail/related_changes/serializer/belongs_to.rb', line 28 def serialize return if association.name.to_s.underscore.singularize == attribute.request_type.to_s.underscore.singularize change.merge_into_root = true unless model_class.relationally_independent? change.add_diff( attribute: attribute_name, old: find_record(attribute.diff[0]), new: find_record(attribute.diff[1]), rank: 3, source: self.class.name ) end |