need_label 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -3,6 +3,21 @@
3
3
  Need_label outputs a class attribute to the indispensable input item of rails.
4
4
  An indispensable input can be expressed by combining with css.
5
5
 
6
+ = How to use
7
+
8
+ (1) Add your Gemfile.
9
+ gem "need_label"
10
+
11
+ (2) Add validation on your model.
12
+ class User < ActiveRecord::Base
13
+ validates :name, :presence => true
14
+ end
15
+
16
+ (3) Add css. (Example)
17
+ label.need-label{
18
+ border-bottom: inset 2px red;
19
+ }
20
+
6
21
  == Copyright
7
22
 
8
23
  Copyright (c) 2011 Yamamoto Kazuhisa. See LICENSE.txt for
data/Rakefile CHANGED
@@ -1,49 +1 @@
1
- # encoding: utf-8
2
-
3
- require 'rubygems'
4
- require 'bundler'
5
- begin
6
- Bundler.setup(:default, :development)
7
- rescue Bundler::BundlerError => e
8
- $stderr.puts e.message
9
- $stderr.puts "Run `bundle install` to install missing gems"
10
- exit e.status_code
11
- end
12
- require 'rake'
13
-
14
- require 'jeweler'
15
- Jeweler::Tasks.new do |gem|
16
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
- gem.name = "need_label"
18
- gem.homepage = "http://github.com/kazuhisa/need_label"
19
- gem.license = "MIT"
20
- gem.summary = "output need-label"
21
- gem.description = "A class is outputted about an indispensable input item."
22
- gem.email = "ak.hisashi@gmail.com"
23
- gem.authors = ["Yamamoto Kazuhisa"]
24
- # dependencies defined in Gemfile
25
- end
26
- Jeweler::RubygemsDotOrgTasks.new
27
-
28
- require 'rspec/core'
29
- require 'rspec/core/rake_task'
30
- RSpec::Core::RakeTask.new(:spec) do |spec|
31
- spec.pattern = FileList['spec/**/*_spec.rb']
32
- end
33
-
34
- RSpec::Core::RakeTask.new(:rcov) do |spec|
35
- spec.pattern = 'spec/**/*_spec.rb'
36
- spec.rcov = true
37
- end
38
-
39
- task :default => :spec
40
-
41
- require 'rake/rdoctask'
42
- Rake::RDocTask.new do |rdoc|
43
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
-
45
- rdoc.rdoc_dir = 'rdoc'
46
- rdoc.title = "need_label #{version}"
47
- rdoc.rdoc_files.include('README*')
48
- rdoc.rdoc_files.include('lib/**/*.rb')
49
- end
1
+ require 'bundler/gem_tasks'
@@ -12,13 +12,26 @@ module ActionView
12
12
  end
13
13
  need = false if options.class == Hash && options[:need_label] == false
14
14
  if need && object.present? && object.class.respond_to?(:validators)
15
- need_attributes = object.class.validators.
16
- select{|e| e.is_a? ActiveModel::Validations::PresenceValidator}.map{|e| e.attributes[0]}
15
+ need_attributes = []
16
+ object.class.validators.each do |validator|
17
+ if validator.is_a? ActiveModel::Validations::PresenceValidator
18
+ if validator.options[:if].nil?
19
+ need_attributes << validator
20
+ else
21
+ if validator.options[:if].is_a? Proc
22
+ need_attributes << validator if object.instance_eval(&validator.options[:if])
23
+ elsif validator.options[:if].is_a? String
24
+ need_attributes << validator if object.instance_eval(&eval("proc{#{validator.options[:if]}}"))
25
+ end
26
+ end
27
+ end
28
+ end
29
+ need_attributes.map!{|e| e.attributes[0]}
17
30
  if need_attributes.index(method.to_sym)
18
31
  if content_or_options.present? && content_or_options.class == Hash && content_or_options[:class].present?
19
- content_or_options[:class] = content_or_options[:class] + " need-label"
32
+ content_or_options[:class] = content_or_options[:class] + ' need-label'
20
33
  else
21
- options[:class] = "need-label"
34
+ options[:class] = 'need-label'
22
35
  end
23
36
  end
24
37
  end
@@ -0,0 +1,3 @@
1
+ module NeedLabel
2
+ VERSION = '0.2.1'
3
+ end
data/lib/need_label.rb CHANGED
@@ -1,3 +1,2 @@
1
- # -*- coding: utf-8 -*-
2
-
1
+ require 'need_label/version'
3
2
  require 'need_label/helpers'
data/need_label.gemspec CHANGED
@@ -1,77 +1,30 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'need_label/version'
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{need_label}
8
- s.version = "0.1.0"
7
+ s.name = 'need_label'
8
+ s.version = NeedLabel::VERSION
9
+ s.authors = ['Yamamoto Kazuhisa']
10
+ s.email = ['ak.hisashi@gmail.com']
11
+ s.description = %q{A class is outputted about an indispensable input item.}
12
+ s.summary = %q{A class is outputted about an indispensable input item.}
13
+ s.homepage = 'https://github.com/kazuhisa/need_label'
14
+ s.license = 'MIT'
9
15
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = [%q{Yamamoto Kazuhisa}]
12
- s.date = %q{2011-10-31}
13
- s.description = %q{A class is outputted about an indispensable input item.}
14
- s.email = %q{ak.hisashi@gmail.com}
15
- s.extra_rdoc_files = [
16
- "LICENSE.txt",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- ".document",
21
- ".rspec",
22
- "Gemfile",
23
- "Gemfile.lock",
24
- "LICENSE.txt",
25
- "README.rdoc",
26
- "Rakefile",
27
- "VERSION",
28
- "lib/need_label.rb",
29
- "lib/need_label/helpers.rb",
30
- "need_label.gemspec",
31
- "spec/fake_app.rb",
32
- "spec/requests/need_label_spec.rb",
33
- "spec/spec_helper.rb"
34
- ]
35
- s.homepage = %q{http://github.com/kazuhisa/need_label}
36
- s.licenses = [%q{MIT}]
37
- s.require_paths = [%q{lib}]
38
- s.rubygems_version = %q{1.8.6}
39
- s.summary = %q{output need-label}
16
+ s.files = `git ls-files`.split($/)
17
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
19
+ s.require_paths = ['lib']
40
20
 
41
- if s.respond_to? :specification_version then
42
- s.specification_version = 3
43
-
44
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
- s.add_runtime_dependency(%q<rails>, [">= 3.0.7"])
46
- s.add_development_dependency(%q<rspec>, ["~> 2.7.0"])
47
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
48
- s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
49
- s.add_development_dependency(%q<rcov>, [">= 0"])
50
- s.add_development_dependency(%q<sqlite3>, ["~> 1.3.0"])
51
- s.add_development_dependency(%q<rspec-rails>, ["~> 2.7.0"])
52
- s.add_development_dependency(%q<capybara>, ["~> 1.1.0"])
53
- s.add_development_dependency(%q<nokogiri>, ["~> 1.5.0"])
54
- else
55
- s.add_dependency(%q<rails>, [">= 3.0.7"])
56
- s.add_dependency(%q<rspec>, ["~> 2.7.0"])
57
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
58
- s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
59
- s.add_dependency(%q<rcov>, [">= 0"])
60
- s.add_dependency(%q<sqlite3>, ["~> 1.3.0"])
61
- s.add_dependency(%q<rspec-rails>, ["~> 2.7.0"])
62
- s.add_dependency(%q<capybara>, ["~> 1.1.0"])
63
- s.add_dependency(%q<nokogiri>, ["~> 1.5.0"])
64
- end
65
- else
66
- s.add_dependency(%q<rails>, [">= 3.0.7"])
67
- s.add_dependency(%q<rspec>, ["~> 2.7.0"])
68
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
69
- s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
70
- s.add_dependency(%q<rcov>, [">= 0"])
71
- s.add_dependency(%q<sqlite3>, ["~> 1.3.0"])
72
- s.add_dependency(%q<rspec-rails>, ["~> 2.7.0"])
73
- s.add_dependency(%q<capybara>, ["~> 1.1.0"])
74
- s.add_dependency(%q<nokogiri>, ["~> 1.5.0"])
75
- end
21
+ s.add_development_dependency 'bundler', '~> 1.3'
22
+ s.add_development_dependency 'rake'
23
+ s.add_development_dependency 'rspec', '~> 2.13'
24
+ s.add_development_dependency 'rspec-rails'
25
+ s.add_development_dependency 'sqlite3', '~> 1.3.0'
26
+ s.add_development_dependency 'capybara','~> 1.1'
27
+ s.add_development_dependency 'rr'
28
+ s.add_dependency 'rails', '>= 3.0.7'
76
29
  end
77
30
 
data/spec/fake_app.rb CHANGED
@@ -10,14 +10,16 @@ ActiveRecord::Base.establish_connection('test')
10
10
 
11
11
  # config
12
12
  app = Class.new(Rails::Application)
13
- app.config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
14
- app.config.session_store :cookie_store, :key => "_myapp_session"
13
+ app.config.secret_token = '3b7cd727ee24e8444053437c36cc66c4'
14
+ app.config.session_store :cookie_store, :key => '_myapp_session'
15
15
  app.config.active_support.deprecation = :log
16
16
  app.initialize!
17
17
 
18
18
  # routes
19
19
  app.routes.draw do
20
20
  resources :users
21
+ resources :user_with_procs
22
+ resources :user_with_strings
21
23
  end
22
24
 
23
25
  # models
@@ -25,6 +27,14 @@ class User < ActiveRecord::Base
25
27
  validates :name, :presence => true
26
28
  end
27
29
 
30
+ class UserWithProc < ActiveRecord::Base
31
+ validates :name, :presence => true, :if => proc{self.age > 20}
32
+ end
33
+
34
+ class UserWithString < ActiveRecord::Base
35
+ validates :name, :presence => true, :if => 'self.age > 20'
36
+ end
37
+
28
38
  # controllers
29
39
  class ApplicationController < ActionController::Base; end
30
40
 
@@ -35,5 +45,7 @@ Object.const_set(:ApplicationHelper, Module.new)
35
45
  class CreateAllTables < ActiveRecord::Migration
36
46
  def self.up
37
47
  create_table(:users) {|t| t.string :name; t.integer :age}
48
+ create_table(:user_with_procs) {|t| t.string :name; t.integer :age}
49
+ create_table(:user_with_strings) {|t| t.string :name; t.integer :age}
38
50
  end
39
51
  end
@@ -1,7 +1,7 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
- describe "output need-label class" do
4
- describe "instance variable type without options" do
3
+ describe 'need-label is outputted to class.' do
4
+ describe 'instance variable parameter without options' do
5
5
  before do
6
6
  class UsersController < ApplicationController
7
7
  def new
@@ -17,21 +17,17 @@ describe "output need-label class" do
17
17
  ERB
18
18
  end
19
19
  end
20
- visit "/users/new"
20
+ visit '/users/new'
21
21
  end
22
- it "It checks that need-label is outputted." do
23
- doc = Nokogiri::HTML(page.html)
24
- label = doc.xpath("//label[@for='user_name']")
25
- label[0]["class"].should == "need-label"
22
+ it 'It checks that need-label is outputted.' do
23
+ page.has_xpath?("//label[@for='user_name'][@class='need-label']").should be_true
26
24
  end
27
- it "It checks that need-label is not outputted." do
28
- doc = Nokogiri::HTML(page.html)
29
- label = doc.xpath("//label[@for='user_age']")
30
- label[0]["class"].should be_nil
25
+ it 'It checks that need-label is not outputted.' do
26
+ page.has_xpath?("//label[@for='user_age'][not(@class)]").should be_true
31
27
  end
32
28
  end
33
29
 
34
- describe "symbol type without options" do
30
+ describe 'symbol parameter without options' do
35
31
  before do
36
32
  class UsersController < ApplicationController
37
33
  def new
@@ -47,21 +43,17 @@ describe "output need-label class" do
47
43
  ERB
48
44
  end
49
45
  end
50
- visit "/users/new"
46
+ visit '/users/new'
51
47
  end
52
- it "It checks that need-label is outputted." do
53
- doc = Nokogiri::HTML(page.html)
54
- label = doc.xpath("//label[@for='user_name']")
55
- label[0]["class"].should == "need-label"
48
+ it 'It checks that need-label is outputted.' do
49
+ page.has_xpath?("//label[@for='user_name'][@class='need-label']").should be_true
56
50
  end
57
- it "It checks that need-label is not outputted." do
58
- doc = Nokogiri::HTML(page.html)
59
- label = doc.xpath("//label[@for='user_age']")
60
- label[0]["class"].should be_nil
51
+ it 'It checks that need-label is not outputted.' do
52
+ page.has_xpath?("//label[@for='user_age'][not(@class)]").should be_true
61
53
  end
62
54
  end
63
55
 
64
- describe "instance variable type with hidden option" do
56
+ describe 'hidden option' do
65
57
  before do
66
58
  class UsersController < ApplicationController
67
59
  def new
@@ -77,51 +69,17 @@ describe "output need-label class" do
77
69
  ERB
78
70
  end
79
71
  end
80
- visit "/users/new"
72
+ visit '/users/new'
81
73
  end
82
- it "It checks that need-label is outputted." do
83
- doc = Nokogiri::HTML(page.html)
84
- label = doc.xpath("//label[@for='user_name']")
85
- label[0]["class"].should be_nil
74
+ it 'It checks that need-label is outputted.' do
75
+ page.has_no_xpath?("//label[@for='user_name'][@class='need-label']").should be_true
86
76
  end
87
- it "It checks that need-label is not outputted." do
88
- doc = Nokogiri::HTML(page.html)
89
- label = doc.xpath("//label[@for='user_age']")
90
- label[0]["class"].should be_nil
77
+ it 'It checks that need-label is not outputted.' do
78
+ page.has_xpath?("//label[@for='user_age'][not(@class)]").should be_true
91
79
  end
92
80
  end
93
81
 
94
- describe "symbol type with hidden option" do
95
- before do
96
- class UsersController < ApplicationController
97
- def new
98
- @user = User.new
99
- render :inline => <<-ERB
100
- <%= form_for :user do |f| %>
101
- <%= f.label :name, :need_label => false %>
102
- <%= f.text_field :name %>
103
- <%= f.label :age %>
104
- <%= f.text_field :age %>
105
- <%= f.submit "save" %>
106
- <% end %>
107
- ERB
108
- end
109
- end
110
- visit "/users/new"
111
- end
112
- it "It checks that need-label is outputted." do
113
- doc = Nokogiri::HTML(page.html)
114
- label = doc.xpath("//label[@for='user_name']")
115
- label[0]["class"].should be_nil
116
- end
117
- it "It checks that need-label is not outputted." do
118
- doc = Nokogiri::HTML(page.html)
119
- label = doc.xpath("//label[@for='user_age']")
120
- label[0]["class"].should be_nil
121
- end
122
- end
123
-
124
- describe "instance variable type with class" do
82
+ describe 'specifies a class name' do
125
83
  before do
126
84
  class UsersController < ApplicationController
127
85
  def new
@@ -137,28 +95,24 @@ describe "output need-label class" do
137
95
  ERB
138
96
  end
139
97
  end
140
- visit "/users/new"
98
+ visit '/users/new'
141
99
  end
142
- it "It checks that need-label is outputted." do
143
- doc = Nokogiri::HTML(page.html)
144
- label = doc.xpath("//label[@for='user_name']")
145
- label[0]["class"].should == "foo need-label"
100
+ it 'It checks that need-label is outputted.' do
101
+ page.has_xpath?("//label[@for='user_name'][@class='foo need-label']").should be_true
146
102
  end
147
- it "It checks that need-label is not outputted." do
148
- doc = Nokogiri::HTML(page.html)
149
- label = doc.xpath("//label[@for='user_age']")
150
- label[0]["class"].should be_nil
103
+ it 'It checks that need-label is not outputted.' do
104
+ page.has_xpath?("//label[@for='user_age'][not(@class)]").should be_true
151
105
  end
152
106
  end
153
107
 
154
- describe "symbol type with class" do
108
+ describe 'specifies a class and hidden options.' do
155
109
  before do
156
110
  class UsersController < ApplicationController
157
111
  def new
158
112
  @user = User.new
159
113
  render :inline => <<-ERB
160
- <%= form_for :user do |f| %>
161
- <%= f.label :name, :class => "foo" %>
114
+ <%= form_for @user do |f| %>
115
+ <%= f.label :name, :class => "foo", :need_label => false %>
162
116
  <%= f.text_field :name %>
163
117
  <%= f.label :age %>
164
118
  <%= f.text_field :age %>
@@ -167,28 +121,24 @@ describe "output need-label class" do
167
121
  ERB
168
122
  end
169
123
  end
170
- visit "/users/new"
124
+ visit '/users/new'
171
125
  end
172
- it "It checks that need-label is outputted." do
173
- doc = Nokogiri::HTML(page.html)
174
- label = doc.xpath("//label[@for='user_name']")
175
- label[0]["class"].should == "foo need-label"
126
+ it 'It checks that need-label is outputted.' do
127
+ page.has_xpath?("//label[@for='user_name'][@class='foo']").should be_true
176
128
  end
177
- it "It checks that need-label is not outputted." do
178
- doc = Nokogiri::HTML(page.html)
179
- label = doc.xpath("//label[@for='user_age']")
180
- label[0]["class"].should be_nil
129
+ it 'It checks that need-label is not outputted.' do
130
+ page.has_xpath?("//label[@for='user_age'][not(@class)]").should be_true
181
131
  end
182
132
  end
183
133
 
184
- describe "instance variable type with class and hidden options" do
134
+ describe "proc type 'if' validate presence option added." do
185
135
  before do
186
- class UsersController < ApplicationController
136
+ class UserWithProcsController < ApplicationController
187
137
  def new
188
- @user = User.new
138
+ @user_with_proc = UserWithProc.new(:age => 21)
189
139
  render :inline => <<-ERB
190
- <%= form_for @user do |f| %>
191
- <%= f.label :name, :class => "foo", :need_label => false %>
140
+ <%= form_for @user_with_proc do |f| %>
141
+ <%= f.label :name %>
192
142
  <%= f.text_field :name %>
193
143
  <%= f.label :age %>
194
144
  <%= f.text_field :age %>
@@ -197,28 +147,21 @@ describe "output need-label class" do
197
147
  ERB
198
148
  end
199
149
  end
200
- visit "/users/new"
201
- end
202
- it "It checks that need-label is outputted." do
203
- doc = Nokogiri::HTML(page.html)
204
- label = doc.xpath("//label[@for='user_name']")
205
- label[0]["class"].should == "foo"
150
+ visit '/user_with_procs/new'
206
151
  end
207
- it "It checks that need-label is not outputted." do
208
- doc = Nokogiri::HTML(page.html)
209
- label = doc.xpath("//label[@for='user_age']")
210
- label[0]["class"].should be_nil
152
+ it 'It checks that need-label is outputted.' do
153
+ page.has_xpath?("//label[@for='user_with_proc_name'][@class='need-label']").should be_true
211
154
  end
212
155
  end
213
156
 
214
- describe "symbol type with class and hidden options" do
157
+ describe "string type 'if' validate presence option added." do
215
158
  before do
216
- class UsersController < ApplicationController
159
+ class UserWithStringsController < ApplicationController
217
160
  def new
218
- @user = User.new
161
+ @user_with_string = UserWithString.new(:age => 21)
219
162
  render :inline => <<-ERB
220
- <%= form_for :user do |f| %>
221
- <%= f.label :name, :class => "foo", :need_label => false %>
163
+ <%= form_for @user_with_string do |f| %>
164
+ <%= f.label :name %>
222
165
  <%= f.text_field :name %>
223
166
  <%= f.label :age %>
224
167
  <%= f.text_field :age %>
@@ -227,17 +170,10 @@ describe "output need-label class" do
227
170
  ERB
228
171
  end
229
172
  end
230
- visit "/users/new"
231
- end
232
- it "It checks that need-label is outputted." do
233
- doc = Nokogiri::HTML(page.html)
234
- label = doc.xpath("//label[@for='user_name']")
235
- label[0]["class"].should == "foo"
173
+ visit '/user_with_strings/new'
236
174
  end
237
- it "It checks that need-label is not outputted." do
238
- doc = Nokogiri::HTML(page.html)
239
- label = doc.xpath("//label[@for='user_age']")
240
- label[0]["class"].should be_nil
175
+ it 'It checks that need-label is outputted.' do
176
+ page.has_xpath?("//label[@for='user_with_string_name'][@class='need-label']").should be_true
241
177
  end
242
178
  end
243
- end
179
+ end
data/spec/spec_helper.rb CHANGED
@@ -15,5 +15,6 @@ RSpec.configure do |config|
15
15
  config.mock_with :rr
16
16
  config.before :all do
17
17
  CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'users'
18
+ CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'user_with_procs'
18
19
  end
19
20
  end