to_param 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
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.
@@ -0,0 +1,54 @@
1
+ # to_param
2
+
3
+ A one-liner solution for `ActiveRecord::Base#to_param`.
4
+
5
+ ## Usage
6
+
7
+ ### Default format
8
+
9
+ ``` ruby
10
+ class User < ActiveRecord::Base
11
+ # ...
12
+ to_param :name
13
+ end
14
+
15
+ User.create(name: "Linus").to_param # => "1-linus"
16
+ ```
17
+
18
+ ### Custom format
19
+
20
+ ``` ruby
21
+ class User < ActiveRecord::Base
22
+ # ...
23
+ to_param "Feel-lucky-:name"
24
+ end
25
+
26
+ User.create(name: "punk").to_param # => "feel-lucky-punk"
27
+ ```
28
+
29
+ ### Static value
30
+
31
+ ``` ruby
32
+ class User < ActiveRecord::Base
33
+ # ...
34
+ to_param "static"
35
+ end
36
+
37
+ User.create(name: "Linus").to_param # => "static"
38
+ ```
39
+
40
+ ## Note
41
+
42
+ `ActiveRecord::Base.to_param` uses [#parameterize](http://apidock.com/rails/ActiveSupport/Inflector/parameterize) under the hood, so you don't have to worry about escaping.
43
+
44
+ ## How do install
45
+
46
+ [sudo] gem install to_param
47
+
48
+ ## Requirements
49
+
50
+ *to_param* is tested in *OS X 10.8.2* using Ruby *1.9.2* and Rails *3.2*.
51
+
52
+ ## License
53
+
54
+ *to_param* is released under the *MIT license*.
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'ToParam'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,4 @@
1
+ module ToParam
2
+ end
3
+
4
+ require "to_param/railtie"
@@ -0,0 +1,34 @@
1
+ module ToParam
2
+ module Base
3
+ extend ActiveSupport::Concern
4
+ #
5
+ # A one-liner solution for ActiveRecord::Base#to_param
6
+ #
7
+ # @args
8
+ # String A non empty format string
9
+ # Symbol An existing attribute name
10
+ # @example
11
+ # to_param :name
12
+ # to_param ":id-:awesome-name"
13
+ # to_param "static"
14
+ #
15
+ def to_param(args)
16
+ case args
17
+ when String
18
+ define_method "to_param" do
19
+ args.gsub(/:(\w+)/) {|method| send(method[1..-1]).to_s.parameterize }
20
+ end
21
+
22
+ if args.empty?
23
+ raise ArgumentError.new("args can not be an empty string")
24
+ end
25
+ when Symbol
26
+ define_method "to_param" do
27
+ "#{id}-#{send(args).parameterize}"
28
+ end
29
+ else
30
+ raise ArgumentError.new("args can only be string or symbol")
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,13 @@
1
+ require "to_param/base"
2
+
3
+ module ToParam
4
+ class Railtie < ::Rails::Railtie #:nodoc:
5
+ initializer "to_param" do |_|
6
+ ActiveSupport.on_load(:active_record) do
7
+ class << self
8
+ include ToParam::Base
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module ToParam
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_param
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Linus Oleander
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-18 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70152997291200 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.11
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70152997291200
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3
27
+ requirement: &70152997290720 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70152997290720
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec-rails
38
+ requirement: &70152997290160 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70152997290160
47
+ - !ruby/object:Gem::Dependency
48
+ name: database_cleaner
49
+ requirement: &70152997289580 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70152997289580
58
+ description: A one-liner solution for ActiveRecord::Base#to_param
59
+ email:
60
+ - ! 'linus@oleander.nu '
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - lib/to_param/base.rb
66
+ - lib/to_param/railtie.rb
67
+ - lib/to_param/version.rb
68
+ - lib/to_param.rb
69
+ - MIT-LICENSE
70
+ - Rakefile
71
+ - README.md
72
+ homepage: http://github.com/oleander/to_param-rb
73
+ licenses: []
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ segments:
85
+ - 0
86
+ hash: -4010010864914912799
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ segments:
94
+ - 0
95
+ hash: -4010010864914912799
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 1.8.10
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: A one-liner solution for ActiveRecord::Base#to_param
102
+ test_files: []