class Upmin::Attribute

Attributes

model[R]
name[R]

Public Class Methods

new(model, attr_name, options = {}) click to toggle source
# File lib/upmin/attribute.rb, line 6
def initialize(model, attr_name, options = {})
  @model = model
  @name = attr_name.to_sym
end

Public Instance Methods

editable?() click to toggle source
# File lib/upmin/attribute.rb, line 31
def editable?
  case name.to_sym
  when :id
    return false
  when :created_at
    return false
  when :created_on
    return false
  when :updated_at
    return false
  when :updated_on
    return false
  else
    # TODO(jon): Add a way to declare which attributes are editable and which are not later.
    return model.respond_to?("#{name}=")
  end
end
enum_options() click to toggle source
# File lib/upmin/attribute.rb, line 65
def enum_options
  model.class.model_class.defined_enums[name.to_s].keys
end
errors?() click to toggle source
# File lib/upmin/attribute.rb, line 49
def errors?
  return model.errors[name].any?
end
form_id() click to toggle source
# File lib/upmin/attribute.rb, line 57
def form_id
  return "#{model.underscore_name}_#{name}"
end
label_name() click to toggle source
# File lib/upmin/attribute.rb, line 53
def label_name
  return name.to_s.gsub(/_/, " ").capitalize
end
nilable_id() click to toggle source
# File lib/upmin/attribute.rb, line 61
def nilable_id
  return "#{form_id}_is_nil"
end
type() click to toggle source
# File lib/upmin/attribute.rb, line 16
def type
  # TODO(jon): Add a way to override with widgets?
  return @type if defined?(@type)

  # Try to get it from the model_class
  @type = model.class.attribute_type(name)

  # If we still don't know the type, try to infer it from the value
  if @type == :unknown
    @type = infer_type_from_value
  end

  return @type
end
value() click to toggle source
# File lib/upmin/attribute.rb, line 11
def value
  # TODO(jon): Add some way to handle exceptions.
  return model.model.send(name)
end

Private Instance Methods

infer_type_from_value() click to toggle source
# File lib/upmin/attribute.rb, line 71
def infer_type_from_value
  class_sym = value.class.to_s.underscore.to_sym
  if class_sym == :false_class || class_sym == :true_class
    return :boolean
  elsif class_sym == :nil_class
    return :unknown
  elsif class_sym == :fixnum
    return :integer
  elsif class_sym == :big_decimal
    return :decimal
  elsif class_sym == :"active_support/time_with_zone"
    return :datetime
  else
    # This should prevent any classes from being skipped, but we may not have an exhaustive list yet.
    return class_sym
  end
end