Module: Skyline::BelongsToReferable::ClassMethods

Defined in:
lib/skyline/belongs_to_referable.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to_referable(*fields, options = {}) ⇒ void

This method returns an undefined value.

Defines a relationship to a referable.

Parameters:

  • *fields (String, Symbol)

    The referable(s) to create associations to.

Options Hash (options):

  • :allow_nil (true, false) — default: true

    Allow this association to be empty?

See Also:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/skyline/belongs_to_referable.rb', line 64

def belongs_to_referable(*fields)
  options = fields.extract_options!.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 options[: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("      def \#{f}_with_passthrough=(obj)\n        if obj.kind_of?(Skyline::ObjectRef)\n          self.\#{f}_without_passthrough = obj\n        else\n          self.attributes = {\"\#{f}_attributes\" => {:referable_type => obj.class.name, :referable_id => obj.id}}\n        end\n      end\n      alias_method_chain :\#{f}=, :passthrough\n      \n      def \#{f}_attributes=(attributes)\n        referable_params = attributes.delete(\"referable_attributes\")\n        self.previous_referables ||= {}\n        self.previous_referables[:\#{f}] = self.\#{f}.referable.dup if self.\#{f}.andand.referable\n        assign_nested_attributes_for_one_to_one_association(:\#{f}, attributes)\n  \n        # only create and modify referable if it is a Skyline::ReferableUri\n        if self.\#{f} && attributes[:referable_type] == \"Skyline::ReferableUri\"\n          self.\#{f}.referable.reload if self.\#{f}.referable\n          self.\#{f}.referable ||= attributes[:referable_type].constantize.new\n        \n          if referable_params.kind_of?(Hash)\n            referable_params.each do |k, v|\n              self.\#{f}.send(k.to_s + \"=\", v) if self.\#{f}.respond_to?(k.to_s + \"=\")\n            end\n          end\n        end\n      end          \n    END\n  end\nend\n", __FILE__, __LINE__ + 1)