Class: SimpleAdmin::Attributes
- Inherits:
-
Object
- Object
- SimpleAdmin::Attributes
- Defined in:
- lib/simple_admin/attributes.rb
Direct Known Subclasses
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
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Instance Method Summary collapse
-
#attribute(name, options = {}, &block) ⇒ Object
Define or override an attribute for this section.
-
#clear ⇒ Object
Clear the attributes for this section.
-
#content(options = {}, &block) ⇒ Object
Include rendered content inline.
-
#defaults(options = {}) ⇒ Object
Define the default attributes for this section.
-
#initialize(interface, section, options = {}, &block) ⇒ Attributes
constructor
Allows you to specify a set of attributes for a section.
-
#section(options = {}, &block) ⇒ Object
Create a section which may or may not contain sub-sections.
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, ={}, &block) @interface = interface @section = section defaults() instance_eval(&block) if block_given? self end |
Instance Attribute Details
#attributes ⇒ Object (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, ={}, &block) key = "#{name}:#{[: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] = attr[:data] = block attr[:title] = [:title] || name.to_s.titleize attr[:sortable] = [:sortable].nil? || !([:sortable] === false) attr[:editable] = [:editable] === true attr[:mode] = [:mode] attr[:sort_key] = ([:sort_key] || name).to_s attr end |
#clear ⇒ Object
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(={}, &block) cont = {} @attributes << cont cont[:kind] = :content cont[:options] = cont[:data] = block cont[:mode] = [: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(={}) 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.[: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 -= [:except] if [:except] @cols &= [:only] if [:only] @cols.sort_by! {|col| [:only].index(col.to_sym) } if [: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(={}, &block) sect = {} @attributes << sect sect[:kind] = .delete(:kind) || :section sect[:options] = sect[:mode] = [:mode] sect[:attributes] = [] save_attributes = @attributes @attributes = sect[:attributes] instance_eval(&block) if block_given? @attributes = save_attributes sect end |