class Sprig::Reap::Value
Attributes
attribute[R]
raw_value[R]
record[R]
sprig_record[R]
value[R]
Public Class Methods
for(sprig_record, attribute)
click to toggle source
# File lib/sprig/reap/value.rb, line 7 def self.for(sprig_record, attribute) new(sprig_record, attribute).for_sprig_file end
new(sprig_record, attribute)
click to toggle source
# File lib/sprig/reap/value.rb, line 11 def initialize(sprig_record, attribute) @sprig_record = sprig_record @record = sprig_record.record @attribute = attribute @value = record.send(attribute) @raw_value = record.read_attribute(attribute) end
Public Instance Methods
for_sprig_file()
click to toggle source
# File lib/sprig/reap/value.rb, line 19 def for_sprig_file @for_sprig_file ||= dependency? ? sprig_dependency_reference : read_attribute end
Private Instance Methods
association()
click to toggle source
# File lib/sprig/reap/value.rb, line 35 def association @association ||= model.associations.detect { |a| a.foreign_key == normalized_foreign_key } end
dependency?()
click to toggle source
# File lib/sprig/reap/value.rb, line 31 def dependency? normalized_foreign_key.in? model.associations.map(&:foreign_key) end
klass()
click to toggle source
# File lib/sprig/reap/value.rb, line 39 def klass @klass ||= if association.polymorphic? record.send(association.polymorphic_type).constantize else association.klass end end
normalized_foreign_key()
click to toggle source
Normalizes has-and-belongs-to-many attributes, which come in as `tag_ids` We want to check for association FKs matching `tag_id`
# File lib/sprig/reap/value.rb, line 27 def normalized_foreign_key @normalized_foreign_key ||= attribute.singularize end
read_attribute()
click to toggle source
# File lib/sprig/reap/value.rb, line 64 def read_attribute file_attr = FileAttribute.new(value) file_attr.file.try(:sprig_location) || raw_value end
sprig_dependency_reference()
click to toggle source
# File lib/sprig/reap/value.rb, line 47 def sprig_dependency_reference references = Array(value).map do |id| sprig_id = Model.find(klass, id).try(:sprig_id) sprig_record_reference(klass, sprig_id) end # For proper Sprig file formatting, need to return an array for HABTM association.has_and_belongs_to_many? ? references : references.first end
sprig_record_reference(klass, sprig_id)
click to toggle source
# File lib/sprig/reap/value.rb, line 58 def sprig_record_reference(klass, sprig_id) return if sprig_id.nil? "<%= sprig_record(#{klass}, #{sprig_id}).id %>" end