Class: Backend::BeehiveHelper::Beehive

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/backend/beehive_helper.rb

Defined Under Namespace

Classes: Box, Cell

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, template) ⇒ Beehive

Returns a new instance of Beehive.


109
110
111
112
113
114
115
# File 'app/helpers/backend/beehive_helper.rb', line 109

def initialize(name, template)
  @name = name
  @boxes = []
  @cells = {}.with_indifferent_access
  @current_box = nil
  @template = template
end

Instance Attribute Details

#boxesObject (readonly)

Returns the value of attribute boxes


107
108
109
# File 'app/helpers/backend/beehive_helper.rb', line 107

def boxes
  @boxes
end

#cellsObject (readonly)

Returns the value of attribute cells


107
108
109
# File 'app/helpers/backend/beehive_helper.rb', line 107

def cells
  @cells
end

#nameObject (readonly)

Returns the value of attribute name


107
108
109
# File 'app/helpers/backend/beehive_helper.rb', line 107

def name
  @name
end

Instance Method Details

#available_cell_typesObject


181
182
183
# File 'app/helpers/backend/beehive_helper.rb', line 181

def available_cell_types
  available_cells.map(&:second)
end

#available_cellsObject


173
174
175
176
177
178
179
# File 'app/helpers/backend/beehive_helper.rb', line 173

def available_cells
  Cell.controller_types.collect do |c|
    [c.tl, c.to_s]
  end.sort do |a, b|
    a.first.ascii <=> b.first.ascii
  end
end

#cell(name = :details, options = {}, &block) ⇒ Object

Adds a cell in the beehive Adds a box too if not defined


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/helpers/backend/beehive_helper.rb', line 119

def cell(name = :details, options = {}, &block)
  if @current_box
    if block_given?
      raise StandardError, 'No block accepted for cells'
      # options[:content] = @template.capture(&block)
    end
    if @cells.keys.include? name.to_s
      raise StandardError, "A cell with a given name (#{name}) has already been given."
    end
    c = Cell.new(name, options)
    @cells[name] = c
    @current_box << c
  else
    hbox do
      cell(name, options, &block)
    end
  end
end

#find_by_name(name) ⇒ Object


169
170
171
# File 'app/helpers/backend/beehive_helper.rb', line 169

def find_by_name(name)
  @cells.values.find { |c| c.name.to_s == name.to_s }
end

#find_by_type(type) ⇒ Object


165
166
167
# File 'app/helpers/backend/beehive_helper.rb', line 165

def find_by_type(type)
  @cells.values.find { |c| c.type.to_s == type.to_s }
end

#hbox(&block) ⇒ Object


138
139
140
# File 'app/helpers/backend/beehive_helper.rb', line 138

def hbox(&block)
  box(&block)
end

#idObject

Returns ID Must be set manually in app/views/layout/cell like above


157
158
159
# File 'app/helpers/backend/beehive_helper.rb', line 157

def id
  "beehive-#{@name}"
end

#layout(user) ⇒ Object


146
147
148
149
150
151
152
153
# File 'app/helpers/backend/beehive_helper.rb', line 146

def layout(user)
  hash = nil
  if preference = user.preferences.find_by(name: preference_name)
    got = YAML.load(preference.value).deep_symbolize_keys
    hash = got if got[:version] && got[:version] >= FORMAT_VERSION
  end
  hash || to_hash
end

#preference_nameObject


161
162
163
# File 'app/helpers/backend/beehive_helper.rb', line 161

def preference_name
  "beehive.#{@name}"
end

#to_hashObject


142
143
144
# File 'app/helpers/backend/beehive_helper.rb', line 142

def to_hash
  { version: FORMAT_VERSION, boxes: @boxes.map(&:to_hash) }
end