Sha256: 372e0b1384d7b51a6e1e47d63301267794ade7bc8e30cb15509ff837c1c78b2e
Contents?: true
Size: 1.8 KB
Versions: 1
Compression:
Stored size: 1.8 KB
Contents
module Her module Model module Associations class BelongsToAssociation < Association # @private def self.attach(klass, name, attrs) attrs = { :class_name => name.to_s.classify, :name => name, :data_key => name, :foreign_key => "#{name}_id", :path => "/#{name.to_s.pluralize}/:id" }.merge(attrs) klass.associations[:belongs_to] << attrs klass.instance_eval do define_method(name) do cached_name = :"@_her_association_#{name}" cached_data = (instance_variable_defined?(cached_name) && instance_variable_get(cached_name)) cached_data || instance_variable_set(cached_name, Her::Model::Associations::BelongsToAssociation.new(self, attrs)) end end end def build(attributes = {}) @klass.new(attributes) end def create(attributes = {}) resource = build(attributes) @parent.attributes[@name] = resource if resource.save resource end # @private def fetch foreign_key_value = @parent.attributes[@opts[:foreign_key].to_sym] return nil if (@parent.attributes.include?(@name) && @parent.attributes[@name].nil? && @query_attrs.empty?) || foreign_key_value.blank? if @parent.attributes[@name].blank? || @query_attrs.any? path = begin @klass.build_request_path(@parent.attributes.merge(@query_attrs.merge(@klass.primary_key => foreign_key_value))) rescue Her::Errors::PathError return nil end @klass.get_resource("#{path}", @query_attrs) else @parent.attributes[@name] end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
her-0.6.1 | lib/her/model/associations/belongs_to_association.rb |