Class: Ramaze::Form

Inherits:
Object show all
Defined in:
lib/ramaze/helper/form.rb

Direct Known Subclasses

ClassForm, InstanceForm

Defined Under Namespace

Modules: Layer

Constant Summary collapse

DATE_GENERIC =
[
[ :day,   DAYS ],
[ :month, MONTHS ],
[ :year,  YEARS ] ]
TIME_GENERIC =
[
[ :day,   DAYS ],
[ :month, MONTHS ],
[ :year,  YEARS ],
[ :hour,  HOURS ],
[ :min,   MINUTES ],
[ :sec,   SECONDS ] ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ Form

Create new instance of Form plus the layer for the ORM



57
58
59
60
61
62
# File 'lib/ramaze/helper/form.rb', line 57

def initialize(object, options = {})
  @object, @options = object, options
  if layer = options.delete(:layer)
    extend layer
  end
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



15
16
17
# File 'lib/ramaze/helper/form.rb', line 15

def object
  @object
end

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/ramaze/helper/form.rb', line 15

def options
  @options
end

Class Method Details

.pick(object, options = {}) ⇒ Object

TODO:

How _elegant_ ...
Tries to find the right module for extending the Form instance.
It's problematic since the boundaries of what an model instance or model
class looks like is very fuzzy, also a problem is that the ORM may not be
available/required.

Maybe we can abstract that a bit by going through an array of procs for
testing?


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ramaze/helper/form.rb', line 42

def self.pick(object, options = {})
  if defined?(Sequel::Model)
    if object.is_a?(Sequel::Model)
      options[:layer] ||= Layer::Sequel
      InstanceForm.new(object, options)
    elsif object.ancestors.include?(Sequel::Model)
      options[:layer] ||= Layer::Sequel
      ClassForm.new(object, options)
    end
  else
    raise "Unknown ORM for: %p" % object
  end
end

Instance Method Details

#field_for(hash) ⇒ Object

Decide on the strucuture of the tag based on the hash



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ramaze/helper/form.rb', line 76

def field_for(hash)
  return if hash[:primary_key]
  args = args_for(hash)

  inner =
    case type = hash[:type]
    when :integer
      field_integer(*args)
    when :boolean
      field_boolean(*args)
    when :text
      field_textarea(*args)
    when :varchar
      field_input(*args)
    when :date
      field_date(*args)
    when :time
      field_time(*args)
    else
      Log.warn "Unknown field: %p" % hash
      field_input(*args)
    end

  "<label>#{args.first}: </label>\n#{inner}"
end

#to_sObject

Generate and return the final form



65
66
67
68
69
70
71
72
73
# File 'lib/ramaze/helper/form.rb', line 65

def to_s
  out = "<form #{form_attributes}>"
  out << "<fieldset>"
  out << generate
  out << "</fieldset>"
  out << '<input type="submit" />'
  out << '<input type="reset" />'
  out << "</form>"
end