class Selectize::Ajax::Core::Control

Attributes

field[RW]
options[RW]
resource[RW]

Public Class Methods

new(resource, field, options = {}) click to toggle source
# File lib/selectize/ajax/core/control.rb, line 5
def initialize(resource, field, options = {})
  @options = Selectize::Ajax::Core::Settings.new(options).call
  @resource = resource
  @field = field
end

Public Instance Methods

can_add?() click to toggle source
# File lib/selectize/ajax/core/control.rb, line 77
def can_add?
  options.add_path.present? && options.add_modal.present?
end
can_edit?() click to toggle source
# File lib/selectize/ajax/core/control.rb, line 81
def can_edit?
  options.edit_path.present? && options.edit_modal.present?
end
edit_resource_template() click to toggle source
# File lib/selectize/ajax/core/control.rb, line 32
def edit_resource_template
  return unless can_edit?

  @edit_resource ||= options.edit_path if options.edit_path.index('{{id}}').blank?
  @edit_resource ||= options.edit_path.split('/').reverse.map do |part|
    break '{{id}}' unless part.to_i.zero?
    part
  end.reverse.join('/')
  URI.unescape(@edit_resource)
end
field_required?() click to toggle source
# File lib/selectize/ajax/core/control.rb, line 51
def field_required?
  return false unless options.required
  return false if resource_object.blank? || !resource_object.respond_to?(:_validators)
  validators = resource_object._validators[field].map(&:class) rescue []
  [
    validators.include?(ActiveModel::Validations::PresenceValidator),
    validators.include?(ActiveRecord::Validations::PresenceValidator)
  ].any?
rescue
  false
end
has_error?() click to toggle source
# File lib/selectize/ajax/core/control.rb, line 73
def has_error?
  @has_error ||= resource_object? ? resource_object&.errors&.include?(field) : false
end
label() click to toggle source
# File lib/selectize/ajax/core/control.rb, line 28
def label
  (options.label || field.to_s.titleize) if options.label != false
end
resource_id() click to toggle source
# File lib/selectize/ajax/core/control.rb, line 19
def resource_id
  [resource_name, field].join('_')
end
resource_name() click to toggle source
# File lib/selectize/ajax/core/control.rb, line 11
def resource_name
  return resource.to_s.underscore.to_sym unless resource_object?

  class_name = resource.class.name.split('::').last
  return :form if class_name == 'Form'
  class_name.chomp('Form').underscore.to_sym
end
resource_object() click to toggle source
# File lib/selectize/ajax/core/control.rb, line 43
def resource_object
  @model_object ||= if resource_object?
    resource
  else
    resource.to_s.classify.constantize rescue nil
  end
end
value() click to toggle source
# File lib/selectize/ajax/core/control.rb, line 23
def value
  return options.value if options.value.present?
  return resource.send(field) if resource_object? && resource.respond_to?(field)
end
wrap_classes() click to toggle source
# File lib/selectize/ajax/core/control.rb, line 63
def wrap_classes
  return if options.wrap_class == false
  return options.wrap_class if options.wrap_class_only

  classes = [options.wrap_class]
  classes << 'selectize-input-group' if can_add?
  classes << (options.horizontal ? 'col-sm-9' : 'col-sm-12')
  classes.compact.reject(&:blank?).join(' ')
end

Private Instance Methods

resource_object?() click to toggle source
# File lib/selectize/ajax/core/control.rb, line 87
def resource_object?
  @_is_object ||= [resource.is_a?(String), resource.is_a?(Symbol)].none?
end