Class: ARFormWidget
- Inherits:
-
FormWidget
- Object
- Cuca::Widget
- FormWidget
- ARFormWidget
- Includes:
- Cuca::FormElements, Cuca::Generator::Markaby
- Defined in:
- lib/cuca/stdlib/arform.rb
Instance Method Summary collapse
- #fe(type, name) ⇒ Object
-
#fe_for(column_name) ⇒ Object
build a form element for column name.
- #field_enable?(fname) ⇒ Boolean
- #field_hidden?(fname) ⇒ Boolean
- #form ⇒ Object
-
#on_submit ⇒ Object
On submit will pass the model to the callback on the controller.
-
#output(form_name, model, options = {}) ⇒ Object
valid options * :disabled_on_create => [‘field_name_1’, ‘field_name_2’, ..] switch off fields on new records * :diabled_on_update => [‘field_name_1’, ‘field_name_2’, ..] switch off fields on existing records * :save_attribs => [‘attr1’, ‘attr2’] allow to call a setter even if it’s not a db column * ..
-
#validate(variables) ⇒ Object
Validate will check on ActiveRecord validation errors.
Methods included from Cuca::Generator::Markaby
Methods inherited from FormWidget
#before_validate, #get_default_variable, #get_form_variables, posted?, #posted?, run, #setup, #v
Methods inherited from Cuca::Widget
#app, #cgi, #clear, clear_hints, #content, #content=, #controller, define_attr_method, #escape, #escapeHTML, #get_assigns, #hints, #initialize, #log, #params, #query_parameters, #request_method, #request_parameters, run_attr_method, #session, #to_s, #unescape, #unescapeHTML
Constructor Details
This class inherits a constructor from Cuca::Widget
Instance Method Details
#fe(type, name) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/cuca/stdlib/arform.rb', line 123 def fe(type, name) return '' if field_hidden?(name) attribs = {} attribs[:disabled] = '' unless field_enable?(name) r = "" case(type) when :string r << fe_text(name, attribs) when :boolean r << fe_bool(name, attribs) when :integer r << fe_int(name, attribs) when :datetime r << fe_datetime(name, attribs) when :date r << fe_date(name, attribs) when :password r << fe_password(name, attribs) end return r end |
#fe_for(column_name) ⇒ Object
build a form element for column name
146 147 148 149 |
# File 'lib/cuca/stdlib/arform.rb', line 146 def fe_for(column_name) col = @model.column_for_attribute(column_name) fe(col.type, col.name) end |
#field_enable?(fname) ⇒ Boolean
105 106 107 108 109 110 111 |
# File 'lib/cuca/stdlib/arform.rb', line 105 def field_enable?(fname) if @model.new_record? then return @disabled_on_create.include?(fname) ? false : true else return @disabled_on_update.include?(fname) ? false : true end end |
#field_hidden?(fname) ⇒ Boolean
113 114 115 116 117 118 119 |
# File 'lib/cuca/stdlib/arform.rb', line 113 def field_hidden?(fname) if @model.new_record? then return @hidden_on_create.include?(fname) ? true : false else return @hidden_on_update.include?(fname) ? true : false end end |
#form ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/cuca/stdlib/arform.rb', line 153 def form mab do FormErrors(@form_errors) fe_formstart table do @model.class.columns.each do |col| next if field_hidden?(col.name) tr { td { col.name }; td { fe(col.type, col.name) } } end tr { td {} ; td { fe_submit }} end fe_formend end end |
#on_submit ⇒ Object
On submit will pass the model to the callback on the controller
56 57 58 59 |
# File 'lib/cuca/stdlib/arform.rb', line 56 def on_submit controller.send(@form_name+'_submit', @model) unless controller.nil? clear end |
#output(form_name, model, options = {}) ⇒ Object
valid options
-
:disabled_on_create => [‘field_name_1’, ‘field_name_2’, ..]
switch off fields on new records
-
:diabled_on_update => [‘field_name_1’, ‘field_name_2’, ..]
switch off fields on existing records
-
:save_attribs => [‘attr1’, ‘attr2’]
allow to call a setter even if it's not a db column
-
.. options from FormWidgets …
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/cuca/stdlib/arform.rb', line 34 def output(form_name, model, = {}) @model = model @disabled_on_update = [:disabled_on_update] || [] @disabled_on_create = [:disabled_on_create] || [] @hidden_on_update = [:hidden_on_update] || [] @hidden_on_create = [:hidden_on_create] || [] @save_attribs = [:save_attribs] || [] setup if self.respond_to?(:setup) # you might want to use a method for setup [:default_values] = model.attributes.merge([:default_values] || {}) @save_attribs.each do |sa| [:default_values][sa] = model.send(sa.intern) if model.respond_to?(sa.intern) end super(form_name, ) end |
#validate(variables) ⇒ Object
Validate will check on ActiveRecord validation errors
66 67 68 69 70 71 72 73 74 75 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 101 |
# File 'lib/cuca/stdlib/arform.rb', line 66 def validate(variables) form if @_content.empty? # password fields might write hints to the validator... clear @form_errors = {} p = variables p.delete(@submit_name) if @model.new_record? then @disabled_on_create.each { |d| p.delete(d) } @hidden_on_create.each { |d| p.delete(d) } else @disabled_on_update.each { |d| p.delete(d) } @hidden_on_update.each { |d| p.delete(d) } end # don't save empty passwords!! @password_fields ||= [] @password_fields.each do |pwf| p.delete(pwf) if p[pwf].chomp.empty? end # remove possible additional data that model doesn't support to # p.delete_if { |k,v| [email protected]_to?("#{k}=") } column_names = @model.class.columns.map { |c| c.name } p.each do |k,v| @model.send("#{k}=".intern, v) if (column_names.include?(k) && @model.respond_to?("#{k}=")) || @save_attribs.include?(k) end # @model.attributes = p return true if @model.valid? @model.errors.each do |k,v| @form_errors[k] = v end end |