Class: AdminIt::SingleContext

Inherits:
Context
  • Object
show all
Defined in:
lib/admin_it/context/single_context.rb

Direct Known Subclasses

SavableSingleContext, ShowContext

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from AdminIt::Context

Instance Attribute Details

#entityObject

Returns the value of attribute entity.



79
80
81
# File 'lib/admin_it/context/single_context.rb', line 79

def entity
  @entity
end

Class Method Details

.path(entity) ⇒ Object



71
72
73
74
75
76
# File 'lib/admin_it/context/single_context.rb', line 71

def self.path(entity)
  AdminIt::Engine.routes.url_helpers.send(
    "#{resource.name}_path",
    entity
  )
end

.section(*names, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/admin_it/context/single_context.rb', line 44

def self.section(*names, &block)
  @sections ||= {}
  names.ensure_symbols.each do |name|
    if @sections.key?(name)
      section = @sections[name]
    else
      if @sections.empty?
        general = Section.new
        general.name(:general)
        # TODO: require this and other files after I18n config in engine.rb
        #general.display_name(I18n.t('admin_it.collection.no_data'))
        general.display_name('Основные свойства')
        general.fields(*fields.map(&:field_name))
        @sections[:general] = general
      end
      section = Section.new
      section.name(name)
      @sections[name] = section
    end
    section.instance_eval(&block) if block_given?
  end
end

.sectionsObject



40
41
42
# File 'lib/admin_it/context/single_context.rb', line 40

def self.sections
  (@sections ||= {}).values
end

.single?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/admin_it/context/single_context.rb', line 67

def self.single?
  true
end

Instance Method Details

#path(_entity: nil) ⇒ Object



94
95
96
97
# File 'lib/admin_it/context/single_context.rb', line 94

def path(_entity: nil)
  _entity ||= entity
  self.class.path(_entity)
end

#sectionObject



99
100
101
# File 'lib/admin_it/context/single_context.rb', line 99

def section
  @section ||= sections.empty? ? :none : :general
end

#section=(value) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/admin_it/context/single_context.rb', line 103

def section=(value)
  value = value.downcase.to_sym if value.is_a?(String)
  return unless value.is_a?(Symbol)
  if sections.empty?
    return if section != :none
  else
    return unless sections.map(&:name).include?(value)
  end
  @section = section
end

#valuesObject



89
90
91
92
# File 'lib/admin_it/context/single_context.rb', line 89

def values
  return {} if @entity.nil?
  Hash[fields(scope: :readable).map { |f| [f.name, f.read(@entity)] }]
end