Module: Cms::Behaviors::DynamicAttributes::MacroMethods

Defined in:
lib/cms/behaviors/dynamic_attributes.rb

Instance Method Summary collapse

Instance Method Details

#has_dynamic_attributes(options = {}) ⇒ Object

Will make the current class have dynamic attributes.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/cms/behaviors/dynamic_attributes.rb', line 126

def has_dynamic_attributes(options={})
  @has_dynamic_attributes = true
  include InstanceMethods

  # Provide default options
  options[:class_name] ||= self.model_name + 'Attribute'
  options[:table_name] ||= options[:class_name].tableize
  options[:relationship_name] ||= options[:class_name].tableize.to_sym
  options[:foreign_key] ||= self.model_name.foreign_key
  options[:base_foreign_key] ||= self.name.underscore.foreign_key
  options[:name_field] ||= 'name'
  options[:value_field] ||= 'value'
  options[:fields].collect! { |f| f.to_s } unless options[:fields].nil?

  # Init option storage if necessary
  cattr_accessor :dynamic_options
  self.dynamic_options ||= Hash.new

  # Return if already processed.
  return if self.dynamic_options.keys.include? options[:class_name]

  # Attempt to load related class. If not create it
  begin
    options[:class_name].constantize
  rescue
    Object.const_set(options[:class_name], Class.new(ActiveRecord::Base)).class_eval do
      self.table_name = options[:table_name]
      self.mass_assignment_sanitizer = Cms::IgnoreSanitizer.new

      def self.reloadable? #:nodoc:
        false
      end
    end
  end

  # Store options
  self.dynamic_options[options[:class_name]] = options

  # Modify attribute class
  attribute_class = options[:class_name].constantize
  base_class = self.name.underscore.to_sym
  attribute_class.class_eval do
    belongs_to base_class, :foreign_key => options[:base_foreign_key]
    alias_method :base, base_class # For generic access
    attr_accessible :name, :value, "#{base_class.to_s}_id".to_sym
  end

  # Modify main class
  class_eval do
    has_many options[:relationship_name],
             :class_name => options[:class_name],
             :foreign_key => options[:foreign_key],
             :dependent => :destroy

    # The following is only setup once
    unless private_method_defined? :method_missing_without_dynamic_attributes

      # Carry out delayed actions before save
      after_validation :save_modified_dynamic_attributes

      # Make attributes seem real
      alias_method :method_missing_without_dynamic_attributes, :method_missing
      alias_method :method_missing, :method_missing_with_dynamic_attributes

      private

      alias_method :read_attribute_without_dynamic_attributes, :read_attribute
      alias_method :read_attribute, :read_attribute_with_dynamic_attributes
      alias_method :write_attribute_without_dynamic_attributes, :write_attribute
      alias_method :write_attribute, :write_attribute_with_dynamic_attributes

    end
  end


end

#has_dynamic_attributes?Boolean



121
122
123
# File 'lib/cms/behaviors/dynamic_attributes.rb', line 121

def has_dynamic_attributes?
  !!@has_dynamic_attributes
end