Sha256: 776f50e10ad853fafb1a89b905e1a9ec5dbb15cb46e72b73c59e8bbff0dee20b

Contents?: true

Size: 1.28 KB

Versions: 6

Compression:

Stored size: 1.28 KB

Contents

class MenuCell < Cell::Rails
  include ::RademadeAdmin::UriHelper

  class << self
    attr_accessor :current_model, :current_ability
  end

  def root_item
    @uri = root_uri
    @name = t('rademade_admin.home')
    @count = nil
    @ico_class = 'glyphicon glyphicon-home'
    @has_sub_items = false
    @is_active = self.class.current_model.nil?
    render view: :item
  end

  def item(item)
    item_data(item)
    render
  end

  def sub_items(sub_items)
    @sub_items = sub_items
    render
  end

  def sub_item(sub_item)
    item_data(sub_item)
    render
  end

  private

  def item_data(item)
    @is_active = current?(item) || children_current?(item) #todo extract service for current
    @uri = can_read?(item) ? admin_list_uri(item.model) : nil
    @name = item.name
    @count = item.count
    @has_sub_items = item.has_sub_items?
    @sub_items = item.sub_items
  end

  def current?(item)
    self.class.current_model == item.model
  end

  def can_read?(item) # todo move authorize check to uri helper
    self.class.current_ability.can?(:read, item.model)
  end

  def children_current?(item, status = false)
    if item.has_sub_items?
      item.sub_items.each do |sub_item|
        status ||= current?(sub_item) || children_current?(sub_item)
      end
    end
    status
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rademade_admin-0.2.2 app/cells/menu_cell.rb
rademade_admin-0.2.1 app/cells/menu_cell.rb
rademade_admin-0.2.0 app/cells/menu_cell.rb
rademade_admin-0.1.6 app/cells/menu_cell.rb
rademade_admin-0.1.5 app/cells/menu_cell.rb
rademade_admin-0.1.4 app/cells/menu_cell.rb