Module: HasMedia::ClassMethods

Defined in:
lib/has_media.rb

Instance Method Summary collapse

Instance Method Details

#create_many_accessors(context, options) ⇒ Object

create_many_accessors Create needed accessors on master object for multiple relation

Parameters:

  • context (String)
  • options (Hash)


273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/has_media.rb', line 273

def create_many_accessors(context, options)
  define_method(context.to_s.pluralize) do
    media.with_context(context.to_sym).uniq
  end

  module_eval <<-"end;", __FILE__, __LINE__
    def #{context}=(values)
      return if values.blank?
      Array(values).each do |value|
        next if value.nil?
        medium = Medium.new_from_value(self, value, "#{context}", "#{options[:encode]}", "#{options[:only]}")
        media << medium if medium
      end
    end
  end;
end

#create_one_accessors(context, options) ⇒ Object

create_one_accessors Create needed accessors on master object for unique relation

Parameters:

  • context (String)
  • options (Hash)


248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/has_media.rb', line 248

def create_one_accessors(context, options)
  define_method(context) do
    media.with_context(context.to_sym).first
  end

  module_eval <<-"end;", __FILE__, __LINE__
    def #{context}=(value)
      return if value.blank?
      medium = Medium.new_from_value(self, value, "#{context}", "#{options[:encode]}", "#{options[:only]}")
      if medium
        @old_media ||= []
        @old_media += media.with_context("#{context}")
        media << medium
      end
    end
  end;
end

#has_many_media(context, options = {}) ⇒ Object

has_many_media Define a class method to link to several media

Parameters:

  • context, (String)

    the context (or accessor) to link media

  • options, (Hash)

    can be one of : encode, only



155
156
157
158
159
# File 'lib/has_media.rb', line 155

def has_many_media(context, options = {})
  set_relations(context, :has_many)
  set_general_methods
  create_many_accessors(context, options)
end

#has_one_medium(context, options = {}) ⇒ Object

has_one_medium Define a class method to link to a medium

Parameters:

  • context, (String)

    the context (or accessor) to link medium

  • options, (Hash)

    can be one of : encode, only



142
143
144
145
146
# File 'lib/has_media.rb', line 142

def has_one_medium(context, options = {})
  set_relations(context, :has_one)
  set_general_methods
  create_one_accessors(context, options)
end

#set_attributesObject

set_attributes Add media_errors attributes to store medium errors



214
215
216
# File 'lib/has_media.rb', line 214

def set_attributes
  attr_accessor :media_errors
end

#set_callbacksObject

set_callbacks Add callbacks to :

- merge medium errors to class related errors
- destroy medium


205
206
207
208
# File 'lib/has_media.rb', line 205

def set_callbacks
  validate :merge_media_errors
  before_save :remove_old_media
end

#set_general_methodsObject

set_general_methods Add generic methods for has_one_medium and has_many_media Including media_links relation, accessors, callbacks, validation …



166
167
168
169
170
171
172
173
174
175
# File 'lib/has_media.rb', line 166

def set_general_methods
  @methods_present ||= false
  unless @methods_present
    set_media_links_relation
    set_attributes
    set_validate_methods
    set_callbacks
  end
  @methods_present = true
end

set_media_links_relation Declare media_links relation



237
238
239
# File 'lib/has_media.rb', line 237

def set_media_links_relation
  has_many :media_links, :as => :mediated, :dependent => :destroy
end

#set_relations(context, relation) ⇒ Object

set_relations add relation on medium if not exists Also check if a class has a duplicate context

Parameters:

  • context (String)
  • relation (String)

    type, one of :has_many, :has_one



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/has_media.rb', line 185

def set_relations(context, relation)
  @contexts ||= {}
  @contexts[relation] ||= []
  @media_relation_set ||= []
  if @contexts[relation].include?(context)
    raise Exception.new("You should NOT use same context identifier for several has_one or has_many relation to media")
  end
  @contexts[relation] << context
  return if @media_relation_set.include? self
  has_many :media, :through => :media_links, :dependent => :destroy

  @media_relation_set << self
end

#set_validate_methodsObject

set_validate_methods Define merge_media_errors to merge medium errors with errors given on master object.



223
224
225
226
227
228
229
230
231
232
# File 'lib/has_media.rb', line 223

def set_validate_methods
  module_eval <<-"end;", __FILE__, __LINE__
    def merge_media_errors
      self.media_errors ||= []
      self.media_errors.each do |error|
        self.errors.add_to_base(error)
      end
    end
  end;
end