labelize 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +80 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/labelize.gemspec +52 -0
- data/lib/labelize.rb +48 -0
- data/test/helper.rb +10 -0
- data/test/test_labelize.rb +7 -0
- metadata +74 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Juan Maiz
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
= labelize
|
2
|
+
|
3
|
+
This very very simple gem is handy for several things:
|
4
|
+
|
5
|
+
* Simplify creating select tags
|
6
|
+
* Easily outputs a model as a string
|
7
|
+
* Ads to_sentence functionality to a model
|
8
|
+
* Allows searching items by the defined label without changing controllers
|
9
|
+
|
10
|
+
First, define the label of your model (it _must_ be a String column):
|
11
|
+
|
12
|
+
class Country
|
13
|
+
label :name
|
14
|
+
end
|
15
|
+
|
16
|
+
Now you are free of "collects" or "collection_select". Look:
|
17
|
+
|
18
|
+
- form_for(@state) do |f|
|
19
|
+
= f.select :country
|
20
|
+
|
21
|
+
You can do that:
|
22
|
+
|
23
|
+
irb> country = Country.create :name => 'Brazil'
|
24
|
+
irb> puts country
|
25
|
+
Brazil
|
26
|
+
=> nil
|
27
|
+
|
28
|
+
And that:
|
29
|
+
|
30
|
+
irb> Country.to_sentence
|
31
|
+
=> "Brazil, Uruguay and Argentina"
|
32
|
+
|
33
|
+
At last, but not the least, you can find items by name automatically:
|
34
|
+
|
35
|
+
irb> Country.find('Brazil')
|
36
|
+
=> #<Country id: 72, name: "Brazil", ...>
|
37
|
+
|
38
|
+
If you have a specific label for searching (like a permalink column), you can set it as a second parameter:
|
39
|
+
|
40
|
+
class Country
|
41
|
+
label :name, :permalink
|
42
|
+
end
|
43
|
+
|
44
|
+
irb> Country.find('south-corea')
|
45
|
+
=> #<Country id: 31, name: "South Corea", :permalink => 'south-corea' ...>
|
46
|
+
|
47
|
+
It works out-of-the-box with inherited_resources and i think it can be used with has_permalink also.
|
48
|
+
|
49
|
+
== Install
|
50
|
+
|
51
|
+
Just add this to your Gemfile (assuming your source is RubyForge)
|
52
|
+
|
53
|
+
gem 'labelize', '0.1.0'
|
54
|
+
|
55
|
+
And then run
|
56
|
+
|
57
|
+
bundle install
|
58
|
+
|
59
|
+
== Ideas
|
60
|
+
|
61
|
+
This gem is still beta, so please, if you have any thought on were the "labels" idea can also be usefull please feel free to share. My twitter is @joaomilho.
|
62
|
+
|
63
|
+
== Rails version
|
64
|
+
|
65
|
+
Labelize is only tested with Rails 3.0.0.beta4. Upgrade baby!
|
66
|
+
|
67
|
+
== Note on Patches/Pull Requests
|
68
|
+
|
69
|
+
* Fork the project.
|
70
|
+
* Make your feature addition or bug fix.
|
71
|
+
* Add tests for it. This is important so I don't break it in a
|
72
|
+
future version unintentionally.
|
73
|
+
* Commit, do not mess with rakefile, version, or history.
|
74
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
75
|
+
* Send me a pull request. Bonus points for topic branches.
|
76
|
+
|
77
|
+
== Copyright
|
78
|
+
|
79
|
+
Copyright (c) 2010 Juan Maiz. See LICENSE for details.
|
80
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "labelize"
|
8
|
+
gem.summary = %Q{label your models}
|
9
|
+
gem.description = %Q{label your models, so you can have easier selects and other useful stuff}
|
10
|
+
gem.email = "juanmaiz@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/softa/labelize"
|
12
|
+
gem.authors = ["Juan Maiz"]
|
13
|
+
# gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "labelize #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'labelize'
|
data/labelize.gemspec
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{labelize}
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Juan Maiz"]
|
12
|
+
s.date = %q{2010-07-08}
|
13
|
+
s.description = %q{label your models, so you can have easier selects and other useful stuff}
|
14
|
+
s.email = %q{juanmaiz@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"init.rb",
|
27
|
+
"labelize.gemspec",
|
28
|
+
"lib/labelize.rb",
|
29
|
+
"test/helper.rb",
|
30
|
+
"test/test_labelize.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/softa/labelize}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.6}
|
36
|
+
s.summary = %q{label your models}
|
37
|
+
s.test_files = [
|
38
|
+
"test/helper.rb",
|
39
|
+
"test/test_labelize.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
+
else
|
48
|
+
end
|
49
|
+
else
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
data/lib/labelize.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
class ActionView::Helpers::FormBuilder
|
2
|
+
def select(method, choices=nil, options = {}, html_options = {})
|
3
|
+
choices ||= method.to_s.camelize.constantize.options
|
4
|
+
@template.select(@object_name, method, choices, options.merge(:object => @object), html_options)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
module Labelize
|
9
|
+
def self.included(base)
|
10
|
+
base.extend(InitialClassMethods)
|
11
|
+
end
|
12
|
+
module InitialClassMethods
|
13
|
+
def label lbl,finder_lbl=nil
|
14
|
+
cattr_accessor :_label
|
15
|
+
cattr_accessor :_finder_label
|
16
|
+
self._label = lbl
|
17
|
+
self._finder_label = finder_lbl || lbl
|
18
|
+
extend(ClassMethods)
|
19
|
+
include InstanceMethods
|
20
|
+
end
|
21
|
+
end
|
22
|
+
module ClassMethods
|
23
|
+
def options
|
24
|
+
all.map(&:to_option)
|
25
|
+
end
|
26
|
+
def to_sentence
|
27
|
+
all.to_sentence
|
28
|
+
end
|
29
|
+
def find(*args)
|
30
|
+
f = args.first
|
31
|
+
if f.is_a? String and f.to_i == 0
|
32
|
+
send("find_by_#{self._finder_label}",*args)
|
33
|
+
else
|
34
|
+
super *args
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module InstanceMethods
|
40
|
+
def to_option
|
41
|
+
[ to_s, id ]
|
42
|
+
end
|
43
|
+
def to_s
|
44
|
+
send(self._label)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
ActiveRecord::Base.send(:include, Labelize)
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: labelize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Juan Maiz
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-08 00:00:00 -03:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: label your models, so you can have easier selects and other useful stuff
|
22
|
+
email: juanmaiz@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- .document
|
32
|
+
- .gitignore
|
33
|
+
- LICENSE
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- VERSION
|
37
|
+
- init.rb
|
38
|
+
- labelize.gemspec
|
39
|
+
- lib/labelize.rb
|
40
|
+
- test/helper.rb
|
41
|
+
- test/test_labelize.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/softa/labelize
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.6
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: label your models
|
72
|
+
test_files:
|
73
|
+
- test/helper.rb
|
74
|
+
- test/test_labelize.rb
|