formula 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/formula.rb +136 -33
  2. metadata +3 -3
data/lib/formula.rb CHANGED
@@ -2,64 +2,167 @@ module Formula
2
2
 
3
3
  require 'formula/railtie' if defined?(Rails)
4
4
 
5
- mattr_accessor :container_tag
6
- @@container_tag = :div
5
+ # Default class assigned to input (<div class="input">...</div>).
6
+ mattr_accessor :input_class
7
+ @@input_class = 'input'
7
8
 
8
- mattr_accessor :hints_tag
9
- @@hints_tag = :div
9
+ # Default class assigned to error (<div class="error">...</div>).
10
+ mattr_accessor :error_class
11
+ @@error_class = 'error'
10
12
 
11
- mattr_accessor :errors_tag
12
- @@errors_tag = :div
13
+ # Default class assigned to hint (<div class="hint">...</div>).
14
+ mattr_accessor :hint_class
15
+ @@hint_class = 'hint'
13
16
 
17
+ # Default tag assigned to input (<div class="input">...</div>).
18
+ mattr_accessor :input_tag
19
+ @@input_tag = :div
20
+
21
+ # Default tag assigned to error (<div class="error">...</div>).
22
+ mattr_accessor :error_tag
23
+ @@error_tag = :div
24
+
25
+ # Default tag assigned to hint (<div class="hint">...</div>).
26
+ mattr_accessor :hint_tag
27
+ @@hint_tag = :div
28
+
29
+ # Default size used on inputs (<input ... size="50" />).
14
30
  mattr_accessor :default_input_size
15
31
  @@default_input_size = 50
16
32
 
33
+ # Default cols used on textarea (<textarea ... cols="50" />).
17
34
  mattr_accessor :default_textarea_cols
18
35
  @@default_textarea_cols = 50
19
36
 
37
+ # Default cols used on textarea (<textarea ... rows="5" />).
20
38
  mattr_accessor :default_textarea_rows
21
- @@default_textarea_rows = 3
39
+ @@default_textarea_rows = 5
22
40
 
23
41
  class FormulaFormBuilder < ActionView::Helpers::FormBuilder
24
42
 
25
- def as(method, options = {})
26
- type = @object.column_for_attribute(method)
43
+
44
+ # Generate a suitable form input for a given method by performing introspection on the type.
45
+ #
46
+ # Options:
47
+ #
48
+ # * :as - override the default type used (:url, :email, :phone, :password, :number, :text)
49
+ # * :label - override the default label used ('Name:', 'URL:', etc.)
50
+ # * :error - override the default error used ('invalid', 'incorrect', etc.)
51
+ # * :class - add custom classes to the container ('grid-04', 'grid-08', etc.)
52
+
53
+ def input(method, options = {})
54
+ options[:as] ||= as(method, options)
55
+ options[:error] ||= error(method, options)
56
+
57
+ components = []
27
58
 
28
- case type
29
- when :string then
30
- return :url if method.to_s =~ /url/
31
- return :email if method.to_s =~ /email/
32
- return :phone if method.to_s =~ /phone/
33
- return :password if method.to_s =~ /password/
34
- when :integer then
35
- return :number
36
- when :float then
37
- return :number
38
- when :decimal then
39
- return :number
59
+ components << self.label(method, options[:label])
60
+
61
+ case options[:as]
62
+ when :text then components << self.text_area(method)
63
+ when :string then components << self.text_field(method)
64
+ when :password then components << self.password_field(method)
65
+ when :url then components << self.url_field(method)
66
+ when :email then components << self.email_field(method)
67
+ when :phone then components << self.phone_field(method)
68
+ when :number then components << self.number_field(method)
69
+ when :date then components << self.date_select(method)
70
+ when :time then components << self.time_select(method)
71
+ when :datetime then components << self.datetime_select(method)
40
72
  end
41
73
 
42
- return :text
74
+ components << @template.content_tag(::Formula.hint_tag, options[:hint], :class => ::Formula.hint_class) if options[:hint]
75
+ components << @template.content_tag(::Formula.error_tag, options[:error], :class => ::Formula.error_class) if options[:error]
76
+
77
+ @template.content_tag(:div, :class => options[:class]) do
78
+ @template.content_tag(::Formula.input_tag, :class => ::Formula.input_class) do
79
+ components.join
80
+ end
81
+ end
82
+ end
83
+
84
+
85
+ # Generate a suitable form association for a given method by performing introspection on the type.
86
+ #
87
+ # Options:
88
+ #
89
+ # * :as - override the default type used (:url, :email, :phone, :password, :number, :text)
90
+ # * :label - override the default label used ('Name:', 'URL:', etc.)
91
+ # * :error - override the default error used ('invalid', 'incorrect', etc.)
92
+ # * :class - add custom classes to the container ('grid-04', 'grid-08', etc.)
93
+
94
+ def association(method, options = {}, &block)
43
95
  end
96
+
97
+
98
+ private
44
99
 
45
- def input(method, options = {}, &block)
46
- options[:as] ||= as(method, options)
47
- options[:errors] ||= @object.errors[method]
48
-
49
- components = []
100
+
101
+ def type(method)
102
+ column = @object.column_for_attribute(method) if @object.respond_to?(:column_for_attribute)
103
+ return column.type if column
104
+ end
105
+
106
+
107
+ # Introspection on the field and method to determine how to render a method. The method is
108
+ # used to generate form element types.
109
+ #
110
+ # Returns:
111
+ #
112
+ # * :url - for columns containing 'url'
113
+ # * :email - for columns containing 'email'
114
+ # * :phone - for columns containing 'phone'
115
+ # * :password - for columns containing 'password'
116
+ # * :number - for integer, float, or decimal columns
117
+
118
+ # * :string - for all other cases
119
+
120
+ def as(method, options = {})
121
+ type = type(method)
50
122
 
51
- components << label(:method, options[:label])
52
- components << @template.content_tag(::Formula.hints_tag, options[:hint], :class => 'hints') if options[:hints]
53
- components << @template.content_tag(::Formula.errors_tag, options[:errors], :class => 'errors') if options[:errors]
123
+ if type == :string or type == nil
124
+ case method
125
+ when /url/ then return :url
126
+ when /email/ then return :email
127
+ when /phone/ then return :phone
128
+ when /password/ then return :password
129
+ end
130
+ end
54
131
 
55
- @template.content_tag(::Formula.container_tag, :class => options[:class]) do
56
- components.join
132
+ case type
133
+ when :string then return :string
134
+ when :integer then return :number
135
+ when :float then return :number
136
+ when :decimal then return :number
137
+ when :timestamp then return :datetime
138
+ when :datetime then return :datetime
139
+ when :date then return :date
140
+ when :time then return :time
141
+ when :text then return :text
57
142
  end
143
+
144
+ return :string
58
145
  end
59
146
 
60
- def association(method, options = {}, &block)
147
+
148
+ # Generate error messages by combining all errors on an object into a comma seperated string
149
+ # representation.
150
+ #
151
+ # Returns:
152
+ #
153
+ # * "name "
154
+ # * :email - for string columns named 'email'
155
+ # * :phone - for string columns named 'phone'
156
+ # * :password - for string columns named 'password'
157
+ # * :number - for integer, float, or decimal columns
158
+ # * :text - for all other cases
159
+
160
+ def error(method, options = {})
161
+ @object.errors[method].to_sentence
61
162
  end
62
163
 
164
+ public
165
+
63
166
  def formula_fields_for(record_or_name_or_array, *args, &block)
64
167
  options = args.extract_options!
65
168
  options[:builder] ||= self.class
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kevin Sylvestre
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-09 00:00:00 -05:00
17
+ date: 2010-11-10 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20