Module: Motr::Mods

Defined in:
lib/motr/mods.rb,
lib/motr/mods/scopeable.rb,
lib/motr/mods/sluggable.rb

Defined Under Namespace

Modules: Scopeable, Sluggable

Instance Method Summary collapse

Instance Method Details

#modded_with(*mods) ⇒ Object Also known as: motr, mod_with

modded_with provides the “hook” functionality to mod a model/class with a particular mod. Multiple mods can be applied at once, with options.

Examples:

Mod a model with the sluggable mod


class Awesome < ActiveRecord::Base
  modded_with :sluggable, :fields => :title, :as => :slug
end

Parameters:

  • *mods (Array)

    Argument list of mods, and/or options



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
# File 'lib/motr/mods.rb', line 17

def modded_with(*mods)
  
  include Motr::Mods::Scopeable
  
  options  = mods.extract_options!
  modules  = mods.map(&:to_sym).uniq
  
  modules.each do |mod|  
    const_name = mod.to_s.classify
    next unless Motr::Mods.const_defined?(:"#{const_name}")
    
    self.motr_mods << mod
    const_incl     = Motr::Mods.const_get(:"#{const_name}")        
    
    # Apply passed options to modules where applicable.
    # 
    if const_incl.instance_variable_defined?("@_mod_options")        
      mod_opts = const_incl.instance_variable_get("@_mod_options")

      new_opts = mod_opts.inject({}) do |hsh, option|
        next hsh unless options.key?(option)
        hsh.merge!(option => options.delete(option))
      end
      
      (class << self; self; end).class_eval do            
        define_method("#{mod}_mod_options"){ motr_mod_options[mod.to_s] }
        define_method("#{mod}_mod_options="){ |opts| motr_mod_options[mod.to_s] = opts }                       
      end
      
      write_inheritable_attribute(:motr_mod_options, {}) if motr_mod_options.nil?
      motr_mod_options[mod.to_s] = new_opts
      
    end

    if const_incl.instance_variable_defined?("@_mod_apply_callback")
      self.class_eval(&const_incl.instance_variable_get("@_mod_apply_callback"))
    end
    
    include const_incl
    Motr.track_mod(mod, self.name.to_s)
    
  end
  
end

#motr_mod_optionsObject



62
63
64
# File 'lib/motr/mods.rb', line 62

def motr_mod_options
  read_inheritable_attribute(:motr_mod_options)
end