Module: Nomo::Model::ClassMethods

Defined in:
lib/nomo/model.rb

Instance Method Summary collapse

Instance Method Details

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/nomo/model.rb', line 34

def has_page(name, options = {})
  options = Nomo.default_options.merge(options)
  page_method        = :"#{name}_page"
  modify_page_method = :"modify_#{name}_page"

  define_method page_method do
    @nomo_pages ||= {}
    @nomo_pages[name] ||= Nomo::Page.new(self, name, options)
  end

  define_method modify_page_method do
    page = send(page_method)
    page.modify(modified_by: self)
  end

  after_update modify_page_method, options.fetch(:update, {}) unless options.has_key?(:on) && ![*options[:on]].include?(:update)
  after_touch  modify_page_method, options.fetch(:touch, {})  unless options.has_key?(:on) && ![*options[:on]].include?(:touch)
end

#modifies(association, name, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/nomo/model.rb', line 53

def modifies(association, name, options = {})
  options = Nomo.default_options.merge(options)
  modify_page_method = :"modify_#{association}_#{name}_page"

  define_method modify_page_method do
    for record in [*send(association)]
      page = record.send(:"#{name}_page")
      page.modify(modified_by: self)
    end
  end

  after_create  modify_page_method, options.fetch(:create, {})  unless options.has_key?(:on) && ![*options[:on]].include?(:create)
  after_update  modify_page_method, options.fetch(:update, {})  unless options.has_key?(:on) && ![*options[:on]].include?(:update)
  after_destroy modify_page_method, options.fetch(:destroy, {}) unless options.has_key?(:on) && ![*options[:on]].include?(:destroy)
  after_touch   modify_page_method, options.fetch(:touch, {})   unless options.has_key?(:on) && ![*options[:on]].include?(:touch)
end