Module: HasMedia::ClassMethods
- Defined in:
- lib/has_media.rb
Instance Method Summary collapse
-
#create_many_accessors(context, options) ⇒ Object
create_many_accessors Create needed accessors on master object for multiple relation.
-
#create_one_accessors(context, options) ⇒ Object
create_one_accessors Create needed accessors on master object for unique relation.
-
#has_many_media(context, options = {}) ⇒ Object
has_many_media Define a class method to link to several media.
-
#has_one_medium(context, options = {}) ⇒ Object
has_one_medium Define a class method to link to a medium.
-
#set_attributes ⇒ Object
set_attributes Add media_errors attributes to store medium errors.
-
#set_callbacks ⇒ Object
set_callbacks Add callbacks to : - merge medium errors to class related errors - destroy medium.
-
#set_general_methods ⇒ Object
set_general_methods Add generic methods for has_one_medium and has_many_media Including media_links relation, accessors, callbacks, validation …
-
#set_media_links_relation ⇒ Object
set_media_links_relation Declare media_links relation.
-
#set_relations(context, relation) ⇒ Object
set_relations add relation on medium if not exists Also check if a class has a duplicate context.
-
#set_validate_methods ⇒ Object
set_validate_methods Define merge_media_errors to merge medium errors with errors given on master object.
Instance Method Details
#create_many_accessors(context, options) ⇒ Object
create_many_accessors Create needed accessors on master object for multiple relation
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, ) 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}", "#{[:encode]}", "#{[: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
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, ) 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}", "#{[:encode]}", "#{[: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
155 156 157 158 159 |
# File 'lib/has_media.rb', line 155 def has_many_media(context, = {}) set_relations(context, :has_many) set_general_methods create_many_accessors(context, ) end |
#has_one_medium(context, options = {}) ⇒ Object
has_one_medium Define a class method to link to a medium
142 143 144 145 146 |
# File 'lib/has_media.rb', line 142 def has_one_medium(context, = {}) set_relations(context, :has_one) set_general_methods create_one_accessors(context, ) end |
#set_attributes ⇒ Object
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_callbacks ⇒ Object
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_methods ⇒ Object
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 ⇒ Object
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
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_methods ⇒ Object
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 |