Class: SimpleAdmin::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_admin/attributes.rb

Direct Known Subclasses

Filters

Constant Summary collapse

SKIPPED_COLUMNS =

Which columns to skip when automatically rendering a form without any fields specified

[:created_at, :updated_at, :created_on, :updated_on, :lock_version, :version]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface, section, options = {}, &block) ⇒ Attributes

Allows you to specify a set of attributes for a section

Available options:

:except An array of excluded attribute names as symbols
:only An array of attribute names for this view


15
16
17
18
19
20
21
# File 'lib/simple_admin/attributes.rb', line 15

def initialize(interface, section, options={}, &block)
  @interface = interface
  @section = section
  defaults(options)
  instance_eval(&block) if block_given?
  self
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/simple_admin/attributes.rb', line 6

def attributes
  @attributes
end

Instance Method Details

#attribute(name, options = {}, &block) ⇒ Object

Define or override an attribute for this section

Available options:

:sortable +true+ or +false+ (defaults to +true+)
:sort_key a column name used when sorting this column (defaults to the column for this attribute)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/simple_admin/attributes.rb', line 36

def attribute(name, options={}, &block)
  key = "#{name}:#{options[:mode]}"
  attr = @attributes_hash[key]
  unless attr
    attr = {}
    @attributes << attr
    @attributes_hash[key] = attr
  end
  attr[:kind] = :attribute
  attr[:attribute] = name.to_sym
  attr[:options] = options
  attr[:data] = block
  attr[:title] = options[:title] || name.to_s.titleize
  attr[:sortable] = options[:sortable].nil? || !(options[:sortable] === false)
  attr[:editable] = options[:editable] === true
  attr[:mode] = options[:mode]
  attr[:sort_key] = (options[:sort_key] || name).to_s
  attr
end

#clearObject

Clear the attributes for this section



24
25
26
27
# File 'lib/simple_admin/attributes.rb', line 24

def clear
  @attributes = []
  @attributes_hash = {}
end

#content(options = {}, &block) ⇒ Object

Include rendered content inline.

This is only used when displaying a form.



60
61
62
63
64
65
66
67
68
# File 'lib/simple_admin/attributes.rb', line 60

def content(options={}, &block)
  cont = {}
  @attributes << cont
  cont[:kind] = :content
  cont[:options] = options
  cont[:data] = block
  cont[:mode] = options[:mode]
  cont
end

#defaults(options = {}) ⇒ Object

Define the default attributes for this section

If the current section is a form it will only use content columns and will skip timestamps and primary key fields.

Available options:

:except An array of excluded attribute names as symbols
:only An array of attribute names for this view


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/simple_admin/attributes.rb', line 110

def defaults(options={})
  clear
  return @cols if defined?(@cols)
  if @section.section == :form
    reflections = @interface.constant.reflections rescue []
    association_columns = reflections.map do |name, ref|
      if ref.macro == :belongs_to && ref.options[:polymorphic] != true
        name.to_sym
      end
    end.compact

    content_columns = @interface.constant.content_columns rescue []
    @cols = content_columns.map {|col| col.name.to_sym }
    @cols += association_columns
    @cols -= SKIPPED_COLUMNS
    @cols.compact!
  else
    @cols = @interface.constant.columns.map{|col| col.name.to_sym } rescue []
  end
  @cols -= options[:except] if options[:except]
  @cols &= options[:only] if options[:only]
  @cols.sort_by! {|col| options[:only].index(col.to_sym) } if options[:only]
  @cols.each {|col| attribute(col.to_s) }
end

#section(options = {}, &block) ⇒ Object

Create a section which may or may not contain sub-sections.

Note: this works better if you first clear the attributes. For example:

attributes do
  clear
  section :kind => :fieldset, :legend => "Primary Address" do
    attribute :address
    section :class => 'csz' do
      attribute :city
      attribute :state
      attribute :zip
    end
  end
end


85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/simple_admin/attributes.rb', line 85

def section(options={}, &block)
  sect = {}
  @attributes << sect
  sect[:kind] = options.delete(:kind) || :section
  sect[:options] = options
  sect[:mode] = options[:mode]
  sect[:attributes] = []
  save_attributes = @attributes
  @attributes = sect[:attributes]
  instance_eval(&block) if block_given?
  @attributes = save_attributes
  sect
end