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.


104
105
106
107
108
109
110
# File 'app/helpers/backend/beehive_helper.rb', line 104

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


102
103
104
# File 'app/helpers/backend/beehive_helper.rb', line 102

def boxes
  @boxes
end

#cellsObject (readonly)

Returns the value of attribute cells


102
103
104
# File 'app/helpers/backend/beehive_helper.rb', line 102

def cells
  @cells
end

#nameObject (readonly)

Returns the value of attribute name


102
103
104
# File 'app/helpers/backend/beehive_helper.rb', line 102

def name
  @name
end

Instance Method Details

#available_cell_typesObject


176
177
178
# File 'app/helpers/backend/beehive_helper.rb', line 176

def available_cell_types
  available_cells.map(&:second)
end

#available_cellsObject


168
169
170
171
172
173
174
# File 'app/helpers/backend/beehive_helper.rb', line 168

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


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/helpers/backend/beehive_helper.rb', line 114

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


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

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

#find_by_type(type) ⇒ Object


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

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

#hbox(&block) ⇒ Object


133
134
135
# File 'app/helpers/backend/beehive_helper.rb', line 133

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

#idObject

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


152
153
154
# File 'app/helpers/backend/beehive_helper.rb', line 152

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

#layout(user) ⇒ Object


141
142
143
144
145
146
147
148
# File 'app/helpers/backend/beehive_helper.rb', line 141

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

#preference_nameObject


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

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

#to_hashObject


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

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