class AdminIt::Context

Constants

CONTEXT_REGEXP

Attributes

context_name[R]
controller_class[RW]
controller[R]
parent[R]
template[R]
toolbar[R]
top_menu[R]

Public Class Methods

collection?() click to toggle source
# File lib/admin_it/context/context.rb, line 63
def self.collection?
  false
end
confirm_destroy?() click to toggle source
# File lib/admin_it/context/context.rb, line 59
def self.confirm_destroy?
  @confirm_destroy.nil? ? true : @confirm_destroy == true
end
create(context_name, _resource) click to toggle source
# File lib/admin_it/context/context.rb, line 40
def self.create(context_name, _resource)
  _resource.ensure_instance_of!(Resource)
  base = self
  Class.new(base) do
    @resource = _resource
    @context_name = context_name
    @entity_class = @resource.entity_class
    @confirm_destroy = @resource.confirm_destroy?

    import_data_module(base)

    @fields = Hash[
      _resource.fields(scope: :all).map { |f| [f.field_name, f] }
    ]

    before_configure if respond_to?(:before_configure)
  end
end
entity_path?() click to toggle source
# File lib/admin_it/context/context.rb, line 71
def self.entity_path?
  false
end
new(from, params: nil, store: nil, parent_init: false) click to toggle source
# File lib/admin_it/context/context.rb, line 100
def initialize(from, params: nil, store: nil, parent_init: false)
  @children = []

  run_callbacks :initialize do
    if from.is_a?(self.class.controller_class)
      @controller = from
    elsif from.is_a?(Context)
      @controller = from.controller
      unless parent_init == true
        self.parent = from
        from.instance_variable_get(:@children) << self
      end
      params ||= {}
      store ||= {}
    end

    @fields = self.class.fields(scope: :all).map { |f| f.new }

    if store.nil?
      session = controller.session
      store = session[:admin_it] ||= {}
      store = store[resource.name] ||= {}
      store = store[name] ||= {}
    end

    params = controller.request.params if params.nil?
    params = Hash[params.map { |k, v| [k.to_sym, v] }]

    run_callbacks :load, arguments: { params: params, store: store } do
      load_context unless parent_init == true
    end
  end
end
single?() click to toggle source
# File lib/admin_it/context/context.rb, line 67
def self.single?
  false
end
url(*args, **params) click to toggle source
# File lib/admin_it/context/context.rb, line 75
def self.url(*args, **params)
  context = nil
  args.reject! { |arg| arg.is_a?(Context) ? context = arg : false }
  url = context.nil? ? path(*args) : context.path(*args)
  params = context.nil? ? params : context.url_params(**params)
  if params.key?(:parent) && params[:parent].is_a?(Context)
    params[:parent] = params[:parent].to_link
  end
  unless params.empty?
    url << '?' << params.map { |k, v| "#{k}=#{v}" }.join('&')
    url = URI.escape(url)
  end
  url
end

Public Instance Methods

begin_render(template) click to toggle source
# File lib/admin_it/context/context.rb, line 222
def begin_render(template)
  @template = template
  @toolbar = Helpers::Toolbar.new(template)
  unless child?
    @top_menu = Helpers::TopMenu.new(template, class: 'navbar-nav')
  end
end
child?() click to toggle source
# File lib/admin_it/context/context.rb, line 218
def child?
  parent.is_a?(Context)
end
end_render(template) click to toggle source
# File lib/admin_it/context/context.rb, line 230
    def end_render(template)
      render
      request = AdminIt::Request.get(controller.request)
      request["admin_it_#{resource.name}_toolbar"] = template.capture do
        @toolbar.render
      end
      unless child?
        request['admin_it_top_menu'] = template.capture { @top_menu.render }
#        @children.each { |c| c.end_render(template) }
      end
    end
field(name) click to toggle source
# File lib/admin_it/context/context.rb, line 168
def field(name)
  name = name.ensure_symbol
  @fields.find { |f| f.name == name }
end
fields(scope: :visible) click to toggle source
# File lib/admin_it/context/context.rb, line 147
def fields(scope: :visible)
  values = @fields
  if scope.is_a?(Hash)
    if scope.key?(:editor)
      return values.select { |f| f.editor == scope[:editor] }
    end
  end
  case scope
  when nil, :all then values
  when :visible then values.select { |f| f.visible? }
  when :hidden then values.select { |f| !f.visible? }
  when :readable then values.select { |f| f.readable? }
  when :writable then values.select { |f| f.writable? }
  when :sortable then values.select { |f| f.sortable? }
  when :with_labels then values.select { |f| f.show_label? }
  when :without_labels then values.select { |f| !f.show_label? }
  when *Field::TYPES then values.select { |f| f.type == scope }
  else values
  end
end
layout() click to toggle source
# File lib/admin_it/context/context.rb, line 183
def layout
  @layout ||= ''
end
layout=(value) click to toggle source
# File lib/admin_it/context/context.rb, line 187
def layout=(value)
  value = value.to_sym if value.is_a?(String)
  return unless value.is_a?(Symbol)
  @layout =
    case value
    when :dialog then 'dialog'
    else ''
    end
end
name() click to toggle source
# File lib/admin_it/context/context.rb, line 143
def name
  @name ||= self.class.context_name
end
parent=(value) click to toggle source
# File lib/admin_it/context/context.rb, line 197
def parent=(value)
  return if value.nil?
  if value.is_a?(Context)
    @parent = value
  elsif value.is_a?(String)
    m = CONTEXT_REGEXP.match(value)
    unless m.nil?
      r = AdminIt.resources[m[:resource_name].downcase.to_sym]
      return if r.nil?
      c = r[m[:context_name].downcase.to_sym]
      return if c.nil?
      @parent = c.new(self, parent_init: true)
      unless m[:identity_value].nil?
        @parent.entity = @parent.load_entity(identity: m[:identity_value])
      end
    end
  else
    @parent = nil
  end
end
partial(name, **locals) click to toggle source
# File lib/admin_it/context/context.rb, line 258
def partial(name, **locals)
  Partial.new(name, **locals)
end
render() click to toggle source
# File lib/admin_it/context/context.rb, line 242
def render()
  func = self.class.instance_variable_get(:@render)
  return if func.nil?
  instance_exec(self, &func)
end
save(**params) click to toggle source
# File lib/admin_it/context/context.rb, line 173
def save(**params)
  return if controller.nil?
  session = controller.session
  store = session[:admin_it] ||= {}
  store = store[resource.name] ||= {}
  run_callbacks :save, arguments: [{ params: params }] do
    store[name] = params
  end
end
url_for(*args, **params) click to toggle source
# File lib/admin_it/context/context.rb, line 253
def url_for(*args, **params)
  return nil if @template.nil?
  @template.url_for(*args, url_params(**params))
end
url_params(**params) click to toggle source
# File lib/admin_it/context/context.rb, line 248
def url_params(**params)
  params.merge!(parent: @parent.to_link) unless @parent.nil?
  params
end

Protected Instance Methods

load_context() click to toggle source
# File lib/admin_it/context/context.rb, line 268
def load_context; end