Class: Upmin::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/upmin/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, attr_name, options = {}) ⇒ Attribute

Returns a new instance of Attribute.



6
7
8
9
# File 'lib/upmin/attribute.rb', line 6

def initialize(model, attr_name, options = {})
  @model = model
  @name = attr_name.to_sym
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/upmin/attribute.rb', line 3

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/upmin/attribute.rb', line 4

def name
  @name
end

Instance Method Details

#editable?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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_optionsObject



65
66
67
# File 'lib/upmin/attribute.rb', line 65

def enum_options
  model.class.model_class.defined_enums[name.to_s].keys
end

#errors?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/upmin/attribute.rb', line 49

def errors?
  return model.errors[name].any?
end

#form_idObject



57
58
59
# File 'lib/upmin/attribute.rb', line 57

def form_id
  return "#{model.underscore_name}_#{name}"
end

#label_nameObject



53
54
55
# File 'lib/upmin/attribute.rb', line 53

def label_name
  return name.to_s.gsub(/_/, " ").capitalize
end

#nilable_idObject



61
62
63
# File 'lib/upmin/attribute.rb', line 61

def nilable_id
  return "#{form_id}_is_nil"
end

#typeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

#valueObject



11
12
13
14
# File 'lib/upmin/attribute.rb', line 11

def value
  # TODO(jon): Add some way to handle exceptions.
  return model.model.send(name)
end