Class: Sidebar

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/sidebar.rb

Overview

This class cannot be autoloaded since other sidebar classes depend on it.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.fieldsObject



52
53
54
# File 'app/models/sidebar.rb', line 52

def self.fields
  @fields ||= []
end

Class Method Details

.apply_staging_on_active!Object



81
82
83
84
85
86
87
88
# File 'app/models/sidebar.rb', line 81

def self.apply_staging_on_active!
  Sidebar.transaction do
    Sidebar.find_each do |s|
      s.active_position = s.staged_position
      s.save!
    end
  end
end

.description(desc = nil) ⇒ Object



56
57
58
59
60
61
62
# File 'app/models/sidebar.rb', line 56

def self.description(desc = nil)
  if desc
    @description = desc
  else
    @description || ""
  end
end

.display_name(new_dn = nil) ⇒ Object



72
73
74
75
# File 'app/models/sidebar.rb', line 72

def self.display_name(new_dn = nil)
  @display_name = new_dn if new_dn
  @display_name || short_name.humanize
end

.ordered_sidebarsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/sidebar.rb', line 13

def self.ordered_sidebars
  os = []
  Sidebar.valid.each do |s|
    if s.staged_position
      os[s.staged_position] = ((os[s.staged_position] || []) << s).uniq
    elsif s.active_position
      os[s.active_position] = ((os[s.active_position] || []) << s).uniq
    end
    if s.active_position.nil? && s.staged_position.nil?
      s.destroy # neither staged nor active: destroy. Full stop.
    end
  end
  os.flatten.compact
end

.path_nameObject



68
69
70
# File 'app/models/sidebar.rb', line 68

def self.path_name
  to_s.underscore
end

.purgeObject



28
29
30
# File 'app/models/sidebar.rb', line 28

def self.purge
  delete_all("active_position is null and staged_position is null")
end

.setting(key, default = nil, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/sidebar.rb', line 32

def self.setting(key, default = nil, options = {})
  key = key.to_s

  return if instance_methods.include?(key)

  fields << SidebarField.build(key, default, options)

  send(:define_method, key) do
    if config.key? key
      config[key]
    else
      default
    end
  end

  send(:define_method, "#{key}=") do |newval|
    config[key] = newval
  end
end

.short_nameObject



64
65
66
# File 'app/models/sidebar.rb', line 64

def self.short_name
  to_s.underscore.split(/_/).first
end

Instance Method Details

#admin_stateObject



134
135
136
137
138
139
140
141
# File 'app/models/sidebar.rb', line 134

def admin_state
  if active_position && (staged_position == active_position || staged_position.nil?)
    return :active
  end
  return :will_change_position if active_position != staged_position

  raise "Unknown admin_state: active: #{active_position}, staged: #{staged_position}"
end

#content_partialObject



124
125
126
# File 'app/models/sidebar.rb', line 124

def content_partial
  "/#{self.class.path_name}/content"
end

#descriptionObject



112
113
114
# File 'app/models/sidebar.rb', line 112

def description
  self.class.description
end

#display_nameObject



120
121
122
# File 'app/models/sidebar.rb', line 120

def display_name
  self.class.display_name
end

#fieldmap(field = nil) ⇒ Object



104
105
106
107
108
109
110
# File 'app/models/sidebar.rb', line 104

def fieldmap(field = nil)
  if field
    self.class.fieldmap[field.to_s]
  else
    self.class.fieldmap
  end
end

#fieldsObject



100
101
102
# File 'app/models/sidebar.rb', line 100

def fields
  self.class.fields
end

#html_idObject



94
95
96
# File 'app/models/sidebar.rb', line 94

def html_id
  "#{short_name}-#{id}"
end

#parse_request(_contents, _params) ⇒ Object



98
# File 'app/models/sidebar.rb', line 98

def parse_request(_contents, _params); end

#publishObject



90
91
92
# File 'app/models/sidebar.rb', line 90

def publish
  self.active_position = staged_position
end

#short_nameObject



116
117
118
# File 'app/models/sidebar.rb', line 116

def short_name
  self.class.short_name
end

#to_locals_hashObject



128
129
130
131
132
# File 'app/models/sidebar.rb', line 128

def to_locals_hash
  fields.reduce(sidebar: self) do |hash, field|
    hash.merge(field.key => field.current_value(self))
  end
end