erector-form_for 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
26
26
  s.add_development_dependency 'rack_csrf'
27
27
  s.add_development_dependency 'sequel'
28
28
  s.add_development_dependency 'sqlite3'
29
+ s.add_development_dependency 'sinatra-r18n'
29
30
 
30
31
  s.files = `git ls-files`.split("\n")
31
32
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -52,7 +52,7 @@ module Erector
52
52
  end
53
53
 
54
54
  def form_input field, attrs = {}
55
- widget WrappedInput.new(@object, @object_name, field, attrs)
55
+ widget WrappedInput.new(@app, @object, @object_name, field, attrs)
56
56
  end
57
57
 
58
58
  private
@@ -77,15 +77,25 @@ module Erector
77
77
  EQUIVALENCIES = {:string => :text, :datetime_local => :'datetime-local', :phone => :tel, :fax => :tel}
78
78
  attr_reader :id
79
79
 
80
- def initialize kind, object, object_name, column, opts
80
+ def initialize app, kind, object, object_name, column, opts
81
+ @type = kind.to_sym
82
+ @app = app
83
+ @column = column
84
+ @object_name = object_name
81
85
  opts[:name] ||= object_name ? "#{object_name}[#{column}]" : column
82
86
  opts[:id] ||= [object_name, column].compact.join('-').dasherize
83
87
  opts[:value] ||= Hash === object ? object[column] : object.send(column)
84
- @type = kind.to_sym
85
- @label = opts.delete(:label) || column.to_s.titleize
88
+ @label = opts.delete(:label) || default_label
86
89
  super opts
87
90
  end
88
91
 
92
+ def default_label
93
+ return @column.to_s.titleize unless @app.respond_to?(:t)
94
+ @app.t.form_for.labels[@object_name][@column] |
95
+ @app.t.models.user.attributes[@column] |
96
+ @column.to_s.titleize
97
+ end
98
+
89
99
  def content
90
100
  case @type
91
101
  when :boolean
@@ -132,7 +142,8 @@ module Erector
132
142
  class WrappedInput < Erector::Widget
133
143
  needs :as, :id => nil, :required => false
134
144
 
135
- def initialize object, object_name, column, opts
145
+ def initialize app, object, object_name, column, opts
146
+ @app = app
136
147
  assigns = {}
137
148
 
138
149
  [:as, :required].each do |key|
@@ -170,7 +181,7 @@ module Erector
170
181
 
171
182
  case @as = @as.to_sym
172
183
  when :hidden, :string, :text, :boolean, :email, :color, :date, :datetime, :'datetime-local', :datetime_local, :email, :file, :image, :month, :number, :password, :range, :search, :tel, :phone, :fax, :time, :url, :week, :select, :radio
173
- @widget = Input.new @as, object, object_name, column, opts.merge(:required => @required)
184
+ @widget = Input.new @app, @as, object, object_name, column, opts.merge(:required => @required)
174
185
  else
175
186
  raise ArgumentError, ":as => #{@as.inspect} is not a valid option"
176
187
  end
@@ -1,3 +1,3 @@
1
1
  module FormErector
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,13 @@
1
+ models:
2
+ user:
3
+ attributes:
4
+ name: Nombre
5
+ bio: Biografía
6
+ confirmed: Confirmado
7
+
8
+ form_for:
9
+ labels:
10
+ user:
11
+ bio: Biografía
12
+ confirmed: ¿Confirmado?
13
+
@@ -388,5 +388,45 @@ describe 'form erector' do
388
388
  it { page.should have_xpath "//form/fieldset/ol/li/fieldset/ol/li/label", :text => 'Best' }
389
389
  it { page.should have_xpath "//form/fieldset/ol/li/fieldset/ol/li/label/input[@value='best' and @checked='checked']" }
390
390
  end
391
+
392
+ describe 'translations' do
393
+ describe 'by model attributes' do
394
+ before do
395
+ new_user.stub!(:bio).and_return('Lorem ipsum...')
396
+ app.instance_eval do
397
+ register Sinatra::R18n
398
+ set :default_locale, 'es'
399
+ set :translations, "#{File.dirname __FILE__}/fixtures"
400
+ end
401
+
402
+ app.user_form(new_user) do
403
+ fields do
404
+ form_input :name
405
+ form_input :bio
406
+ form_input :confirmed
407
+ form_input :any
408
+ end
409
+ end
410
+ get '/users/new'
411
+ puts last_response.body
412
+ end
413
+
414
+ it 'should use model attribute translations' do
415
+ page.should have_css "label[for=user-name]", :text => 'Nombre'
416
+ end
417
+
418
+ it 'should use form_for label translation' do
419
+ page.should have_css "label[for=user-bio]", :text => 'Biografía'
420
+ end
421
+
422
+ it 'should use form_for label translation overriding model attribute' do
423
+ page.should have_css "label[for=user-confirmed]", :text => '¿Confirmado?'
424
+ end
425
+
426
+ it 'should resort to titleize column' do
427
+ page.should have_css "label[for=user-any]", :text => 'Any'
428
+ end
429
+ end
430
+ end
391
431
  end
392
432
  end
@@ -5,6 +5,7 @@ require 'rack/test'
5
5
  require 'rack/csrf'
6
6
  require 'sqlite3'
7
7
  require 'sequel'
8
+ require 'sinatra/r18n'
8
9
 
9
10
  $:.unshift File.join(File.dirname( __FILE__), '..', 'lib')
10
11
  require 'erector/form_for'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: erector-form_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-09-30 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: erector
16
- requirement: &70307239220860 !ruby/object:Gem::Requirement
16
+ requirement: &2165548480 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70307239220860
24
+ version_requirements: *2165548480
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sinatra
27
- requirement: &70307239220260 !ruby/object:Gem::Requirement
27
+ requirement: &2165548060 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70307239220260
35
+ version_requirements: *2165548060
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sinatra-trails
38
- requirement: &70307239219560 !ruby/object:Gem::Requirement
38
+ requirement: &2165547640 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70307239219560
46
+ version_requirements: *2165547640
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: i18n
49
- requirement: &70307239218940 !ruby/object:Gem::Requirement
49
+ requirement: &2165547220 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70307239218940
57
+ version_requirements: *2165547220
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: activesupport
60
- requirement: &70307239218120 !ruby/object:Gem::Requirement
60
+ requirement: &2165546720 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '3.0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70307239218120
68
+ version_requirements: *2165546720
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
- requirement: &70307239217480 !ruby/object:Gem::Requirement
71
+ requirement: &2165546300 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70307239217480
79
+ version_requirements: *2165546300
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &70307239201960 !ruby/object:Gem::Requirement
82
+ requirement: &2165545840 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70307239201960
90
+ version_requirements: *2165545840
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: capybara
93
- requirement: &70307239201400 !ruby/object:Gem::Requirement
93
+ requirement: &2165545420 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70307239201400
101
+ version_requirements: *2165545420
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: rack-test
104
- requirement: &70307239200900 !ruby/object:Gem::Requirement
104
+ requirement: &2165545000 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *70307239200900
112
+ version_requirements: *2165545000
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rack_csrf
115
- requirement: &70307239200360 !ruby/object:Gem::Requirement
115
+ requirement: &2165544580 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *70307239200360
123
+ version_requirements: *2165544580
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: sequel
126
- requirement: &70307239199860 !ruby/object:Gem::Requirement
126
+ requirement: &2165544160 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ! '>='
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *70307239199860
134
+ version_requirements: *2165544160
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: sqlite3
137
- requirement: &70307239199380 !ruby/object:Gem::Requirement
137
+ requirement: &2165543740 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ! '>='
@@ -142,7 +142,18 @@ dependencies:
142
142
  version: '0'
143
143
  type: :development
144
144
  prerelease: false
145
- version_requirements: *70307239199380
145
+ version_requirements: *2165543740
146
+ - !ruby/object:Gem::Dependency
147
+ name: sinatra-r18n
148
+ requirement: &2165543320 !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ! '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ type: :development
155
+ prerelease: false
156
+ version_requirements: *2165543320
146
157
  description: Form helper inspired by erector targeting html5
147
158
  email:
148
159
  - macarui@gmail.com
@@ -157,6 +168,7 @@ files:
157
168
  - form_erector.gemspec
158
169
  - lib/erector/form_for.rb
159
170
  - lib/erector/form_for/version.rb
171
+ - spec/fixtures/es.yml
160
172
  - spec/form_erector_spec.rb
161
173
  - spec/spec_helper.rb
162
174
  homepage: http://github.com/maca/erector-form_for
@@ -184,5 +196,6 @@ signing_key:
184
196
  specification_version: 3
185
197
  summary: Form helper inspired by erector targeting html5
186
198
  test_files:
199
+ - spec/fixtures/es.yml
187
200
  - spec/form_erector_spec.rb
188
201
  - spec/spec_helper.rb