ffmike-from_param 1.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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,16 @@
1
+ Copyright (c) 2007 Michael Bleigh (http://mbleigh.com/) and Intridea Inc. (http://intridea.com/)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4
+ and associated documentation files (the "Software"), to deal in the Software without
5
+ restriction, including without limitation the rights to use, copy, modify, merge, publish,
6
+ distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7
+ Software is furnished to do so, subject to the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be included in all copies or
10
+ substantial portions of the Software.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13
+ BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,57 @@
1
+ FromParam
2
+ =========
3
+
4
+ This plugin is an addition to ActiveRecord::Base that establishes a better
5
+ convention for finding records based on parameters. It adds a "from_param" class
6
+ method to ActiveRecord::Base as a convention for fetching a model from a URL
7
+ parameter. By default it will find a record based on the to_i of the passed in
8
+ parameter, but storing a parameter in a column is where it becomes especially useful.
9
+
10
+ If you create a 'param' column in your table (or any other column using set_param_column),
11
+ the to_param of your record will automatically be saved to that column whenever
12
+ you save the record, and you will be able to retrieve it using just a simple
13
+ Model.from_param call with the generated to_param. It is probably wise to add an index
14
+ to the param column if you use one.
15
+
16
+ It's time to move away from generated-key-dependence, and this plugin is an attempt
17
+ to make that easy, painless, and work easily within the existing systems.
18
+
19
+ Example
20
+ =======
21
+
22
+ # default behavior
23
+
24
+ class User < ActiveRecord::Base
25
+ def to_param
26
+ "#{id}-#{first_name.downcase}-#{last_name.downcase}"
27
+ end
28
+ end
29
+
30
+ class UsersController < ApplicationController
31
+ # GET /users/1-bobby-mcfarin
32
+ def show
33
+ @user = User.from_param(params[:id]) # => <User id=1 first_name="Bobby" last_name="McFarin">
34
+ end
35
+ end
36
+
37
+ # using a 'param' column, in this case 'slug'. Note the use of String.urlize to make the title URL-friendly.
38
+
39
+ class Post < ActiveRecord::Base
40
+ set_param_column "slug" # defaults to "param"
41
+ def to_param
42
+ "#{created_at.strftime("%Y-%m-%d")}-#{title.urlize}"
43
+ end
44
+ end
45
+
46
+ class PostsController < ApplicationController
47
+ # GET /posts/2008-04-26-from-param-plugin-released
48
+ @post = Post.from_param(params[:id]) # => <Post title="From Param: Plugin Released" created_at="2008-04-26">
49
+ end
50
+
51
+ Resources
52
+ =========
53
+
54
+ GitHub: http://github.com/mbleigh/from_param
55
+ Lighthouse: http://mbleigh.lighthouseapp.com/projects/10478-from-param
56
+
57
+ Copyright (c) 2007 Michael Bleigh (http://mbleigh.com/) and Intridea Inc. (http://intridea.com/), released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the from_param plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the from_param plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'from_param'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
data/lib/from_param.rb ADDED
@@ -0,0 +1,53 @@
1
+ class ActiveRecord::Base
2
+ # Class Methods
3
+ class << self
4
+ # The column that is currently set to be used with
5
+ # the from_param method.
6
+ def param_column
7
+ "param"
8
+ end
9
+ alias :param_column= :param_column
10
+
11
+ # Allows you to set the column that will be used
12
+ # by from_param by default.
13
+ def set_param_column(val)
14
+ define_attr_method :param_column, val
15
+ end
16
+
17
+ # Returns true if the param_column is one of the
18
+ # currently defined columns of your table.
19
+ def param_column?
20
+ column_names.include?(param_column)
21
+ end
22
+
23
+ # Takes a parameter generated by to_param and
24
+ # tried to find a matching record. If param_column
25
+ # is set and exists, uses that as the finder, otherwise
26
+ # uses the id such as in the default Rails to_param.
27
+ def from_param(parameter)
28
+ if param_column?
29
+ send "find_by_#{param_column}", parameter
30
+ else
31
+ find_by_id(parameter.to_i)
32
+ end
33
+ end
34
+ end
35
+
36
+ # Calls param_column? on the class
37
+ def param_column?
38
+ self.class.param_column?
39
+ end
40
+
41
+ # Calls param_column on the class
42
+ def param_column
43
+ self.class.param_column
44
+ end
45
+
46
+ before_save :set_param
47
+ # Automatically called before saving, sets
48
+ # the param_column to the current to_param
49
+ def set_param
50
+ send "#{param_column}=", to_param if param_column?
51
+ end
52
+
53
+ end
@@ -0,0 +1,10 @@
1
+ class String
2
+ # Convert an arbitrary string into a format suitable for use in a URL
3
+ #
4
+ # * Replaces spaces with dashes
5
+ # * Converts to all lower-case
6
+ # * Removes all characters except alphabetic and numeric
7
+ def urlize
8
+ self.gsub(" ","-").downcase.gsub(/[^a-z0-9-]/,"")
9
+ end
10
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'from_param'
2
+ require 'string_extensions'
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe ActiveRecord::Base, "with from_param" do
4
+ before(:each) do
5
+ @paramed = ParameteredModel.new
6
+ end
7
+
8
+ it "#param_column? should be true if there is a param column, false otherwise" do
9
+ ParameteredModel.param_column?.should be_true
10
+ UnparameteredModel.param_column?.should be_false
11
+ end
12
+
13
+ it "should automatically save the to_param to the param field if one exists" do
14
+ @paramed.name = "Angus McIntyre"
15
+ @paramed.save
16
+ ParameteredModel.find(@paramed.id).param.should == "angus-mcintyre"
17
+ end
18
+
19
+ it "should retrieve a model from its to_param" do
20
+ @paramed.name = "Bob McFinchalot!!!"
21
+ @paramed.save
22
+ ParameteredModel.from_param(@paramed.to_param).should == @paramed
23
+ end
24
+
25
+ it "#from_param should work on models that don't have a param attribute using the id instead" do
26
+ unparamed = UnparameteredModel.create(:name => "Something")
27
+ unparamed.to_param.should == "1-something"
28
+ UnparameteredModel.from_param(unparamed.to_param).should == unparamed
29
+ end
30
+
31
+ it "should allow the param column to be set to any column" do
32
+ @paramed = SluggedModel.new
33
+ @paramed.name = "Bob McFinchalot!!!"
34
+ @paramed.save
35
+ SluggedModel.from_param(@paramed.to_param).should == @paramed
36
+ end
37
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,15 @@
1
+ ActiveRecord::Schema.define :version => 0 do
2
+ create_table :parametered_models, :force => true do |t|
3
+ t.column :name, :string
4
+ t.column :param, :string
5
+ end
6
+
7
+ create_table :unparametered_models, :force => true do |t|
8
+ t.column :name, :string
9
+ end
10
+
11
+ create_table :slugged_models, :force => true do |t|
12
+ t.column :name, :string
13
+ t.column :slug, :string
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
2
+
3
+ plugin_spec_dir = File.dirname(__FILE__)
4
+ ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
5
+
6
+ load(File.dirname(__FILE__) + '/schema.rb')
7
+
8
+ class ParameteredModel < ActiveRecord::Base
9
+ def to_param
10
+ name.gsub(" ","-").downcase.gsub(/[^a-z-]/,"")
11
+ end
12
+ end
13
+
14
+ class UnparameteredModel < ActiveRecord::Base
15
+ def to_param
16
+ "#{id}-#{name.gsub(" ","-").downcase.gsub(/[^a-z-]/,"")}"
17
+ end
18
+ end
19
+
20
+ class SluggedModel < ActiveRecord::Base
21
+ set_param_column "slug"
22
+
23
+ def to_param
24
+ name.gsub(" ","-").downcase.gsub(/[^a-z-]/,"")
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "urlize" do
4
+
5
+ it "should replace spaces with dashes" do
6
+ "a b c".urlize.should == "a-b-c"
7
+ end
8
+
9
+ it "should downcase the string" do
10
+ "AbCdE".urlize.should == "abcde"
11
+ end
12
+
13
+ it "should remove all characters except letters, numbers, and dashes" do
14
+ "A_b C#d!-e".urlize.should == "ab-cd-e"
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ffmike-from_param
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - Michael Bleigh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-29 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: This plugin is an addition to ActiveRecord::Base that establishes a better convention for finding records based on parameters. It adds a 'from_param' class method to ActiveRecord::Base as a convention for fetching a model from a URL parameter.
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - MIT-LICENSE
26
+ - Rakefile
27
+ - README
28
+ - init.rb
29
+ - lib/from_param.rb
30
+ - lib/string_extensions.rb
31
+ - rails/init.rb
32
+ - spec/from_param_spec.rb
33
+ - spec/schema.rb
34
+ - spec/spec_helper.rb
35
+ - spec/string_extensions_spec.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/mbleigh/from_param/tree/master
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: Better convention for finding records based on parameters
62
+ test_files: []
63
+