Module: OrbitalFields::Tokenize::ClassMethods

Defined in:
lib/orbital_fields/tokenize.rb

Instance Method Summary collapse

Instance Method Details

#tokenize_associations(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/orbital_fields/tokenize.rb', line 12

def tokenize_associations(*args)
  set_callback :save, :after, :tokenize_callback
  association_symbols = []
  tokenize = args.extract_options!
  tokenize.each do |key, val|
    args << {key => val}
  end
  args.each do |association|
    raise TypeError, "Argument is not a symbol or hash" unless association.class == Symbol || association.class == Hash
    if association.class == Hash
      association_hash = association
      association = association.keys[0]
      raise ArgumentError, "If an argument is a hash, it must have a key named 'display'" unless association_hash[association].has_key?(:display)
      association_display = association_hash[association][:display]
      raise TypeError, "Display argument is not a symbol" unless association_display.class == Symbol
    end

    unless self.current_class.associations.include?(association.to_s)
      raise NoMethodError, "Argument '#{association.to_s}' is not a defined association in the #{self.current_class} class"
    end
    
    association_symbols << association
    
    association_display ||= :name
    
    association_class =  Kernel.const_get(self.current_class.associations[association.to_s].class_name)
    
    unless association_class.attribute_method?(association_display)
      raise ArgumentError, "The #{association_class} class does not have a '#{association_display}' attribute"
    end
    
    setter_method_name = "#{association.to_s.singularize}_tokens="
    getter_method_name = "#{association.to_s.singularize}_tokens"
    json_method_name = "#{association.to_s.singularize}_tokens_json"
    
    define_method(setter_method_name) do |tokens|
      if tokens.blank?
        eval("@#{getter_method_name} = []")
      else
        id_array = tokens.split(",")
        id_array = id_array.uniq
        instance_variable_set("@#{getter_method_name}", id_array)
      end
    end unless method_defined? setter_method_name
    
    define_method(getter_method_name) do
      eval("@#{getter_method_name} ||= []")
      return eval("@#{getter_method_name}.join(',')") unless eval("@#{getter_method_name}.blank?")
      return "" if eval("#{association.to_s}.blank?")
      eval("#{association.to_s}.map(&:id).join(',')")
    end unless method_defined? getter_method_name
    
    define_method(json_method_name) do
      eval("@#{getter_method_name} ||= []")
      return association_class.where(:_id.in => eval("@#{getter_method_name}")).only(:id,association_display).map(&:attributes).to_json unless eval("@#{getter_method_name}.blank?")
      eval("#{association.to_s}.only(:id, :#{association_display.to_s}).map(&:attributes).to_json")
    end unless method_defined? json_method_name
  end # args.each
  self.token_associations =  association_symbols
end