Class: RailsAdmin::Config::Fields::Base

Inherits:
Base
  • Object
show all
Includes:
Hideable
Defined in:
lib/rails_admin/config/fields/base.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#abstract_model, #bindings, #parent, #root

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hideable

#hidden?, #hide, included, #show

Methods inherited from Base

#has_option?, register_class_option, #register_deprecated_instance_option, register_deprecated_instance_option, register_instance_option, #register_instance_option, #with

Constructor Details

#initialize(parent, name, properties) ⇒ Base

Returns a new instance of Base.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rails_admin/config/fields/base.rb', line 22

def initialize(parent, name, properties)
  super(parent)
  
  @defined = false
  @name = name
  @order = 0
  @properties = properties
  @section = parent
  
  extend RailsAdmin::Config::Fields::Groupable
end

Instance Attribute Details

#definedObject

Returns the value of attribute defined.



14
15
16
# File 'lib/rails_admin/config/fields/base.rb', line 14

def defined
  @defined
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/rails_admin/config/fields/base.rb', line 13

def name
  @name
end

#orderObject

Returns the value of attribute order.



14
15
16
# File 'lib/rails_admin/config/fields/base.rb', line 14

def order
  @order
end

#propertiesObject (readonly)

Returns the value of attribute properties.



13
14
15
# File 'lib/rails_admin/config/fields/base.rb', line 13

def properties
  @properties
end

#sectionObject

Returns the value of attribute section.



14
15
16
# File 'lib/rails_admin/config/fields/base.rb', line 14

def section
  @section
end

Class Method Details

.inherited(klass) ⇒ Object



16
17
18
# File 'lib/rails_admin/config/fields/base.rb', line 16

def self.inherited(klass)
  klass.instance_variable_set("@view_helper", :text_field)
end

Instance Method Details

#_html_attributesObject



268
269
270
# File 'lib/rails_admin/config/fields/base.rb', line 268

def _html_attributes
  html_attributes.merge(!default_value.nil? ? { :value => default_value } : {})
end

#association?Boolean

Is this an association

Returns:

  • (Boolean)


211
212
213
# File 'lib/rails_admin/config/fields/base.rb', line 211

def association?
  kind_of?(RailsAdmin::Config::Fields::Association)
end

#editableObject



197
198
199
200
201
202
203
204
# File 'lib/rails_admin/config/fields/base.rb', line 197

def editable
  return false if @properties && @properties[:read_only]
  role = bindings[:view].controller.send(:_attr_accessible_role)
  klass = bindings[:object].class
  whitelist = klass.accessible_attributes(role).map(&:to_s)
  blacklist = klass.protected_attributes(role).map(&:to_s)
  !self.method_name.to_s.in?(blacklist) && (whitelist.any? ? self.method_name.to_s.in?(whitelist) : true)
end

#errorsObject

Reader for validation errors of the bound object



216
217
218
# File 'lib/rails_admin/config/fields/base.rb', line 216

def errors
  bindings[:object].errors[name]
end

#inverse_ofObject



260
261
262
# File 'lib/rails_admin/config/fields/base.rb', line 260

def inverse_of
  nil
end

#method_nameObject



264
265
266
# File 'lib/rails_admin/config/fields/base.rb', line 264

def method_name
  name
end

#nested_formObject

Reader for nested attributes



256
257
258
# File 'lib/rails_admin/config/fields/base.rb', line 256

def nested_form
  false
end

#optional(state = nil, &block) ⇒ Object

Inverse accessor whether this field is required.

See Also:

  • register_instance_option(:required?)


230
231
232
233
234
235
236
# File 'lib/rails_admin/config/fields/base.rb', line 230

def optional(state = nil, &block)
  if !state.nil? || block
    required state.nil? ? proc { false == (instance_eval &block) } : false == state
  else
    optional?
  end
end

#optional=(state) ⇒ Object

Writer to make field optional.

See Also:

  • optional


241
242
243
# File 'lib/rails_admin/config/fields/base.rb', line 241

def optional=(state)
  optional(state)
end

#optional?Boolean

Reader whether field is optional.

Returns:

  • (Boolean)

See Also:

  • register_instance_option(:required?)


223
224
225
# File 'lib/rails_admin/config/fields/base.rb', line 223

def optional?
  not required
end

#renderObject



206
207
208
# File 'lib/rails_admin/config/fields/base.rb', line 206

def render
  bindings[:view].render :partial => partial.to_s, :locals => {:field => self, :form => bindings[:form] }
end

#typeObject

Reader for field’s type



246
247
248
# File 'lib/rails_admin/config/fields/base.rb', line 246

def type
  @type ||= self.class.name.to_s.demodulize.underscore.to_sym
end

#type_css_classObject



38
39
40
# File 'lib/rails_admin/config/fields/base.rb', line 38

def type_css_class
  "#{type}_type"
end

#valueObject

Reader for field’s value



251
252
253
# File 'lib/rails_admin/config/fields/base.rb', line 251

def value
  bindings[:object].safe_send(name)
end

#virtual?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/rails_admin/config/fields/base.rb', line 42

def virtual?
  properties.blank?
end