Module: Skyline::ContentItem::ClassMethods
- Defined in:
- lib/skyline/content_item.rb
Instance Method Summary collapse
-
#referable_content(*fields) ⇒ Object
accepts options as the last parameter options = true/false (default: true).
- #referable_serialized_content(*fields) ⇒ Object
Instance Method Details
#referable_content(*fields) ⇒ Object
accepts options as the last parameter
options[:allow_nil] = true/false (default: true)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/skyline/content_item.rb', line 91 def referable_content(*fields) = fields..reverse_merge(:allow_nil => true) fields.each do |f| self.referable_contents << f belongs_to f, :class_name => "Skyline::ObjectRef", :foreign_key => "#{f}_id", :dependent => :destroy accepts_nested_attributes_for f, :reject_if => proc {|attributes| attributes['referable_type'].blank?}, :allow_destroy => true unless [:allow_nil] # validating on :linked instead of :linked_id here; see: # http://railsforum.com/viewtopic.php?id=30300 # https://rails.lighthouseapp.com/projects/8994/tickets/1943 # https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2815-nested-models-build-should-directly-assign-the-parent validates_presence_of f end self.class_eval(<<-END, __FILE__, __LINE__ + 1) def #{f}_with_passthrough=(obj) if obj.kind_of?(Skyline::ObjectRef) self.#{f}_without_passthrough = obj else self.attributes = {"#{f}_attributes" => {:referable_type => obj.class.name, :referable_id => obj.id}} end end alias_method_chain :#{f}=, :passthrough def #{f}_attributes=(attributes) referable_params = attributes.delete("referable_attributes") self.previous_referables ||= {} self.previous_referables[:#{f}] = self.#{f}.referable.dup if self.#{f}.andand.referable assign_nested_attributes_for_one_to_one_association(:#{f}, attributes) # only create and modify referable if it is a Skyline::ReferableUri if self.#{f} && attributes[:referable_type] == "Skyline::ReferableUri" self.#{f}.referable.reload if self.#{f}.referable self.#{f}.referable ||= attributes[:referable_type].constantize.new if referable_params.kind_of?(Hash) referable_params.each do |k, v| self.#{f}.send(k.to_s + "=", v) if self.#{f}.respond_to?(k.to_s + "=") end end end end END end end |
#referable_serialized_content(*fields) ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/skyline/content_item.rb', line 139 def referable_serialized_content(*fields) fields.each do |f| self.class_eval <<-END def #{f}_attributes=(attr) self.#{f}_id = attr[:referable_id] end END end end |