Class: SemiFormal::Input

Inherits:
Object show all
Defined in:
lib/semi_formal/input.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, name) ⇒ Input

Returns a new instance of Input.



5
6
7
# File 'lib/semi_formal/input.rb', line 5

def initialize(instance, name)
  @instance, @name = instance, name
end

Instance Attribute Details

#instance ⇒ Object (readonly)

Returns the value of attribute instance.



3
4
5
# File 'lib/semi_formal/input.rb', line 3

def instance
  @instance
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/semi_formal/input.rb', line 3

def name
  @name
end

Instance Method Details

#attr_name ⇒ Object



27
28
29
# File 'lib/semi_formal/input.rb', line 27

def attr_name
  instance.class.model_name.singular + "[#{name}]"
end

#call(builder) ⇒ Object Also known as: []



9
10
11
12
13
14
15
# File 'lib/semi_formal/input.rb', line 9

def call(builder)
  this = self
  builder.label do
    span { text this.label_name }
    this.decorate(self)
  end
end

#decorate(builder) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/semi_formal/input.rb', line 35

def decorate(builder)
  this = self
  case field_type
  when :string
    builder.input(:name => attr_name, :type => "text", :value => value)
  when :text
    this = self
    builder.textarea(:name => attr_name) { text(this.value.to_s) }
  end
end

#field_type ⇒ Object



31
32
33
# File 'lib/semi_formal/input.rb', line 31

def field_type
  instance.class.columns_hash[name.to_s].type
end

#label_name ⇒ Object



23
24
25
# File 'lib/semi_formal/input.rb', line 23

def label_name
  name.to_s.titleize
end

#value ⇒ Object



19
20
21
# File 'lib/semi_formal/input.rb', line 19

def value
  instance.send(name)
end