Class: Para::AttributeField::Base
- Inherits:
-
Object
- Object
- Para::AttributeField::Base
- Defined in:
- lib/para/attribute_field/base.rb
Direct Known Subclasses
BooleanField, DatetimeField, EnumField, FileField, FriendlyId, ImageField, PasswordField, RelationField, Translation, WysiwygEditor
Instance Attribute Summary collapse
-
#field_method ⇒ Object
readonly
Returns the value of attribute field_method.
-
#field_type ⇒ Object
readonly
Returns the value of attribute field_type.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .field_option(key, method_name, options = {}) ⇒ Object
- .field_types ⇒ Object
-
.register(*args) ⇒ Object
Registers the class as the responder for a given field type.
Instance Method Summary collapse
- #attribute_column_path ⇒ Object
- #determine_name_and_field_method! ⇒ Object
- #excerptable_value? ⇒ Boolean
- #field_name ⇒ Object
- #field_options ⇒ Object
-
#initialize(model, options = {}) ⇒ Base
constructor
A new instance of Base.
-
#parse_input(params, resource) ⇒ Object
Allows parsing input params before they’re passed to the model, so it can be easy to edit them according to some field type specific behavior.
- #searchable? ⇒ Boolean
- #type?(type) ⇒ Boolean
- #value_for(instance) ⇒ Object
Constructor Details
#initialize(model, options = {}) ⇒ Base
Returns a new instance of Base.
44 45 46 47 48 49 50 51 52 |
# File 'lib/para/attribute_field/base.rb', line 44 def initialize(model, = {}) @model = model @name = .delete(:name) @type = .delete(:type) @field_type = .delete(:field_type) @options = determine_name_and_field_method! end |
Instance Attribute Details
#field_method ⇒ Object (readonly)
Returns the value of attribute field_method.
7 8 9 |
# File 'lib/para/attribute_field/base.rb', line 7 def field_method @field_method end |
#field_type ⇒ Object (readonly)
Returns the value of attribute field_type.
7 8 9 |
# File 'lib/para/attribute_field/base.rb', line 7 def field_type @field_type end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
7 8 9 |
# File 'lib/para/attribute_field/base.rb', line 7 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/para/attribute_field/base.rb', line 7 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/para/attribute_field/base.rb', line 7 def @options end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/para/attribute_field/base.rb', line 7 def type @type end |
Class Method Details
.field_option(key, method_name, options = {}) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/para/attribute_field/base.rb', line 9 def self.field_option(key, method_name, = {}) self. ||= [] self. += [{ key: key, method_name: method_name, options: }] end |
.field_types ⇒ Object
38 39 40 |
# File 'lib/para/attribute_field/base.rb', line 38 def self.field_types @_field_types ||= {} end |
.register(*args) ⇒ Object
Registers the class as the responder for a given field type
Example :
# This will allow looking :my_field or :myfield up and instantiate
# self as the field
class MyField < Para::AttributeField::Base
register :my_field, :myfield, self
end
30 31 32 33 34 35 36 |
# File 'lib/para/attribute_field/base.rb', line 30 def self.register(*args) attribute_class = args.pop args.each do |arg| Base.field_types[arg] = attribute_class end end |
Instance Method Details
#attribute_column_path ⇒ Object
108 109 110 |
# File 'lib/para/attribute_field/base.rb', line 108 def attribute_column_path [name] end |
#determine_name_and_field_method! ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/para/attribute_field/base.rb', line 54 def determine_name_and_field_method! name = @name reference = model.reflect_on_all_associations.find do |association| association.foreign_key == name rescue ArgumentError # This can happen when the association is polymorphic and the foreign key can't # be determined, in this case, we just ignore the association. false end if reference @name = reference.name @field_method = :association else @name = name @field_method = :input end end |
#excerptable_value? ⇒ Boolean
78 79 80 |
# File 'lib/para/attribute_field/base.rb', line 78 def excerptable_value? true end |
#field_name ⇒ Object
104 105 106 |
# File 'lib/para/attribute_field/base.rb', line 104 def field_name name end |
#field_options ⇒ Object
97 98 99 100 101 102 |
# File 'lib/para/attribute_field/base.rb', line 97 def self.class..each_with_object({}) do |params, hash| value = send(params[:method_name]) hash[params[:key]] = value if !value.nil? || params[:options][:allow_nil] end end |
#parse_input(params, resource) ⇒ Object
Allows parsing input params before they’re passed to the model, so it can be easy to edit them according to some field type specific behavior
95 |
# File 'lib/para/attribute_field/base.rb', line 95 def parse_input(params, resource); end |
#searchable? ⇒ Boolean
82 83 84 85 86 87 88 89 |
# File 'lib/para/attribute_field/base.rb', line 82 def searchable? [:searchable] != false && ( %i[string text].include?(type.to_sym) && !name.match(/password/) ) && ( !model.respond_to?(:ransackable_attributes) || model.ransackable_attributes.include?(name.to_s) ) end |
#type?(type) ⇒ Boolean
112 113 114 |
# File 'lib/para/attribute_field/base.rb', line 112 def type?(type) self.type.to_s == type.to_s end |
#value_for(instance) ⇒ Object
74 75 76 |
# File 'lib/para/attribute_field/base.rb', line 74 def value_for(instance) instance.send(name) end |