Class: RubberRing::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/rubber_ring/page.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#edit_modeObject

Returns the value of attribute edit_mode.



3
4
5
# File 'app/models/rubber_ring/page.rb', line 3

def edit_mode
  @edit_mode
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'app/models/rubber_ring/page.rb', line 3

def title
  @title
end

Class Method Details

.add_template(options) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/rubber_ring/page.rb', line 55

def self.add_template(options)
  page, content = get_page_and_content(options, :content)

  last = RubberRing::PageTemplate.all.order(:sort).last
  pt = RubberRing::PageTemplate.where('id = ?', content['index']).first_or_initialize()

  new_pt      = pt.id.nil? ? last.dup : pt.dup
  new_pt.sort = last.sort + 1
  new_pt.save

  return page, new_pt
end

.remove(options, key_to_remove) ⇒ Object



74
75
76
77
78
79
80
# File 'app/models/rubber_ring/page.rb', line 74

def self.remove(options, key_to_remove)
  page = load_first_page(options)
  RubberRing::PageContent
    .where('page_id = ? AND key = ?', page.id, key_to_remove)
    .delete_all
  page
end

.remove_template(options) ⇒ Object



68
69
70
71
72
# File 'app/models/rubber_ring/page.rb', line 68

def self.remove_template(options)
  page, content = get_page_and_content(options, :content)
  RubberRing::PageTemplate.where('id = ?', content['index']).delete_all
  page
end

.save_or_update(options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/rubber_ring/page.rb', line 7

def self.save_or_update(options)
  page, page_content = get_page_and_content(options, :content)

  page_content.keys.each do |key|
    pc = RubberRing::PageContent.where('page_id = ? AND key = ?', page.id, key)
      .first_or_initialize()

    pc.update_attributes(
      key:   key,
      value: page_content[key],
      page:  page
    )

    pc.save
  end

  page
end

.save_or_update_templates(options) ⇒ Object



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
# File 'app/models/rubber_ring/page.rb', line 26

def self.save_or_update_templates(options)
  page, templates = get_page_and_content(options, :content)

  templates.keys.each do |key|
    template = templates[key]
    pt = RubberRing::PageTemplate.where('id = ?', template['index']).first_or_initialize()

    if pt.id.nil?
      pt.update_attributes(
        key:      template['key'],
        template: template['template'],
        sort:     template['sort'],
        tclass:   template['tclass'],
        element:  template['element'],
        page:     page
      )
    else
      # when updating, only sort is allowed to change
      pt.update_attributes(
        sort: template['sort']
      )
    end

    pt.save
  end

  page
end

Instance Method Details

#contentObject

all page_contents as one hash



92
93
94
95
96
97
98
# File 'app/models/rubber_ring/page.rb', line 92

def content
  result = {}
  page_contents.each do |page_content|
    result[page_content.key] = page_content.value
  end
  result
end

#edit_mode?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'app/models/rubber_ring/page.rb', line 87

def edit_mode?
  @edit_mode
end

#group_keys(group_key) ⇒ Object



82
83
84
85
# File 'app/models/rubber_ring/page.rb', line 82

def group_keys(group_key)
  return [] if content.nil?
  (content.select { |key, _| key.match(group_key) }).keys
end