Module: FlexiModel::Fields::ClassMethods

Defined in:
lib/flexi_model/fields.rb

Instance Method Summary collapse

Instance Method Details

#define_field(field, generate_accessors = true) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/flexi_model/fields.rb', line 173

def define_field(field, generate_accessors = true)
  # Add new field
  if generate_accessors
    # Remove existing definition
    remove_flexi_field field.name

    flexi_fields << field

    # Map name and field
    _flexi_fields_by_name[
        (field.name.is_a?(Symbol) ?
            field.name : field.name.to_sym)] = field

    _define_accessors(field)
  else
    none_flexi_fields << field
  end
end

#flexi_field(name, type, options = { }) ⇒ Object Also known as: _ff

Set filed with name, type and default value or proc

name - Field name should be downcased multi words separated by underscore type - Supported type String, Integer, Time, Date options - Hash of options

default   - Default value proc or object
singular  - Singular label
plural    - Plural label

Return Field instance



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/flexi_model/fields.rb', line 131

def flexi_field(name, type, options = { })
  accessible = options.delete(:accessible)
  default    = options.delete(:default)
  singular   = options.delete(:singular) || name.to_s.singularize
  plural     = options.delete(:plural) || name.to_s.pluralize
  _type      = type.is_a?(Symbol) || type.is_a?(String) ? type : type.name


  field            = FlexiField.new(name, _type.to_s.downcase, default)
  field.accessible = accessible
  field.singular   = singular
  field.plural     = plural
  field.options    = options

  opt_accessors = options.delete(:accessors)
  generate_accessors = opt_accessors.nil? ? true : opt_accessors

  define_field(field, generate_accessors)
end

#remove_flexi_field(name) ⇒ Object

Remove field by field name Return list of existing fields



169
170
171
# File 'lib/flexi_model/fields.rb', line 169

def remove_flexi_field(name)
  flexi_fields.reject! { |f| f.name == name }
end

#set_flexi_name_field(field) ⇒ Object

Set field name to determine default name field.



105
106
107
# File 'lib/flexi_model/fields.rb', line 105

def set_flexi_name_field(field)
  self.flexi_name_field = field
end

#set_flexi_partition_id(_id) ⇒ Object

Isolate and group all models under a single administration panel This is useful when we are hosting these models under a single platform.



117
118
119
# File 'lib/flexi_model/fields.rb', line 117

def set_flexi_partition_id(_id)
  self.flexi_partition_id = _id
end

#set_flexi_visible(bool) ⇒ Object

Set current model as visible or invisible to admin panel Setting false won’t publicize this model over admin panel



111
112
113
# File 'lib/flexi_model/fields.rb', line 111

def set_flexi_visible(bool)
  self.flexi_visible = bool
end