Module: Content::ItemAssociationClassMethods

Included in:
Item
Defined in:
lib/content/item_association_class_methods.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(name, options = {}) ⇒ Object Also known as: has_one



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
# File 'lib/content/item_association_class_methods.rb', line 72

def belongs_to(name, options = {})
  raise "name must be singular" unless name.to_s == name.to_s.singularize
  name_id = "#{name}_id".to_sym
  ignored_attributes << name
  serialized_attributes << name_id

  define_method(name_id) do
    self[name_id].to_i
  end

  define_method("#{name_id}=".to_sym) do |val|
    if val.is_a? Content::Item
      self[name] = val
      self[name_id] = val.id.to_i 
    else
      self[name_id] = val.to_i
    end
  end

  define_method(name) do
    self[name] ||= self.class.find(self[name_id].to_i)
  end

  define_method("#{name}=".to_sym) do |val|
    if val.is_a? Content::Item
      val.save! if val.new_record?
      self[name] = val
      self[name_id] = val.id.to_i
    end
  end
end

#field(name_or_ary, field_type = :string) ⇒ Object



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
138
# File 'lib/content/item_association_class_methods.rb', line 106

def field(name_or_ary, field_type = :string)
  names = name_or_ary
  names = [name_or_ary] unless name_or_ary.is_a? Array
  names.each do |name|
    serialized_attributes << name
    field_klass = field_type.to_s.camelcase.constantize

    define_method(name) do
      self[name] = ActiveSupport::JSON.decode(self[name]) if !self[name].is_a?(field_klass) and self[name].is_a? String
      self[name]
    end

    define_method("#{name}=".to_sym) do |val|
      self[name] = val
    end

    define_method("#{name}_changed?".to_sym) do
      self.changed_attributes.has_key? name
    end

    define_method("#{name}_change".to_sym) do
      [self.changed_attributes[name], self[name]] if self.changed_attributes.has_key? name
    end

    define_method("#{name}_was".to_sym) do
      if self.changed_attributes.has_key? name
        self.changed_attributes[name]
      else
        self[name]
      end
    end
  end
end

#fields(*names) ⇒ Object



140
141
142
# File 'lib/content/item_association_class_methods.rb', line 140

def fields(*names)
  field names
end

#has_many(name, options = {}) ⇒ Object



11
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
# File 'lib/content/item_association_class_methods.rb', line 11

def has_many(name, options = {})
  raise "name must be plural" unless name.to_s == name.to_s.pluralize
  name_ids = "#{name.to_s.singularize}_ids".to_sym
  ignored_attributes << name
  serialized_attributes << name_ids

  define_method(name_ids) do
    if instance_variable_get("@#{name}_loaded".to_sym)
      self[name_ids] = self[name].collect(&:id)
      self[name] = nil
      instance_variable_set("@#{name}_loaded".to_sym, false)
    else
      if self[name_ids].nil?
        self[name_ids] = []
      else
        self[name_ids] = ActiveSupport::JSON.decode(self[name_ids]) if !self[name_ids].is_a?(Array) and self[name_ids].is_a? String
      end
    end
    self[name_ids]
  end

  define_method("#{name_ids}=".to_sym) do |val|
    self[name] = nil
    instance_variable_set("@#{name}_loaded".to_sym, false)

    if val.is_a? Content::Item
      self[name_ids] = [val.id.to_i] 
    elsif !val.is_a? Array
      self[name_ids] = [val.to_i]
    else
      self[name_ids] = val
    end
  end

  define_method(name) do
    ary = self[name]
    if ary.nil?
      self[name] = ary = (self.send(name_ids) || []).collect {|id| self.class.find(id.to_i) }
      instance_variable_set("@#{name}_loaded".to_sym, true)
      self[name_ids] = nil
    end
    ary
  end

  define_method("#{name}=".to_sym) do |val|
    if val.is_a? Array
      instance_variable_set("@#{name}_loaded".to_sym, true)
      self[name] = val
      self[name_ids] = nil
    elsif val.nil?
      instance_variable_set("@#{name}_loaded".to_sym, false)
      self[name] = nil
      self[name_ids] = nil
    end
  end

  define_method("#{name}_loaded".to_sym) do
    instance_variable_get("@#{name}_loaded".to_sym)
  end
end

#ignored_attributesObject



3
4
5
# File 'lib/content/item_association_class_methods.rb', line 3

def ignored_attributes
  @ignored_attributes ||= []
end

#index(name, index_type = :lexical) ⇒ Object



144
145
146
# File 'lib/content/item_association_class_methods.rb', line 144

def index(name, index_type = :lexical)
  self.connection.set_index name, index_type
end

#serialized_attributesObject



7
8
9
# File 'lib/content/item_association_class_methods.rb', line 7

def serialized_attributes
  @serialized_attributes ||= []
end