Module: GoogleSpreadsheets::LinkRelations::ClassMethods

Defined in:
lib/google_spreadsheets/link_relations.rb

Instance Method Summary collapse

Instance Method Details

#create_reflection(macro, name, options) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/google_spreadsheets/link_relations.rb', line 54

def create_reflection(macro, name, options)
  # TODO: improve
  if macro == :links_to_many
    reflection = LinkRelationReflection.new(macro, name, options)
    self.reflections = self.reflections.merge(name => reflection)
    reflection
  else
    super
  end
end


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/google_spreadsheets/link_relations.rb', line 36

def defines_links_to_many_finder_method(method_name, relation_name, association_model)
  ivar_name = :"@#{method_name}"

  define_method(method_name) do |options = {}|
    options.assert_valid_keys(:as)
    if instance_variable_defined?(ivar_name)
      instance_variable_get(ivar_name)
    elsif attributes.include?(method_name)
      attributes[method_name]
    else
      model = options[:as].try{|as| as.to_s.safe_constantize } || association_model
      link = self.link.find{|l| l.rel == relation_name }
      path = link.href.slice(%r|^https://spreadsheets\.google\.com(/.*)|, 1)
      instance_variable_set(ivar_name, model.find(:all, from: path))
    end
  end
end


32
33
34
# File 'lib/google_spreadsheets/link_relations.rb', line 32

def links_to_many(name, options = {})
  Builder::LinksToMany.build(self, name, options)
end