public
Description: Rails plugin that adds a from_param class method to ActiveRecord::Base for simple URL-based fetching.
Homepage: http://mbleigh.lighthouseapp.com/projects/10478-from-param
Clone URL: git://github.com/mbleigh/from_param.git
Complete rewrite to include automatic storing and retrieval from a 'param' 
column along with the default functionality. Should make parameterizing models a 
snap!
mbleigh (author)
Sat Apr 26 20:10:57 -0700 2008
commit  0317cb7e6e089d3bfd18546a353fc577c232a4b0
tree    14751cf6c4ff2bdfe4ec91521de8a87dc711f093
parent  722c4c7b276501ff3db10f2a59b0d197f9f7e59f
0
...
1
2
3
4
 
5
6
7
8
 
 
 
 
 
 
 
9
10
11
...
16
17
18
19
 
 
 
20
21
22
23
 
24
25
 
26
27
28
29
 
30
31
 
 
32
33
34
35
36
37
 
38
39
40
41
42
43
 
 
 
44
 
 
 
 
 
45
46
...
1
2
3
 
4
5
6
7
 
8
9
10
11
12
13
14
15
16
17
...
22
23
24
 
25
26
27
28
29
30
 
31
32
 
33
34
35
36
 
37
38
 
39
40
41
 
 
 
 
 
42
43
44
45
 
 
 
46
47
48
49
50
51
52
53
54
55
56
0
@@ -1,11 +1,17 @@
0
 FromParam
0
 =========
0
 
0
-This plugin is a tiny addition to ActiveRecord::Base that establishes a better 
0
+This plugin is an addition to  ActiveRecord::Base that establishes a better 
0
 convention for finding records based on parameters. It adds a "from_param" class
0
 method to ActiveRecord::Base as a convention for fetching a model from a URL
0
 parameter. By default it will find a record based on the to_i of the passed in
0
-parameter, but the real use is by overriding it.
0
+parameter, but storing a parameter in a column is where it becomes especially useful.
0
+
0
+If you create a 'param' column in your table (or any other column using set_param_column),
0
+the to_param of your record will automatically be saved to that column whenever
0
+you save the record, and you will be able to retrieve it using just a simple
0
+Model.from_param call with the generated to_param. It is probably wise to add an index
0
+to the param column if you use one.
0
 
0
 It's time to move away from generated-key-dependence, and this plugin is an attempt
0
 to make that easy, painless, and work easily within the existing systems.
0
@@ -16,31 +22,35 @@ Example
0
   # default behavior
0
 
0
   class User < ActiveRecord::Base
0
-  
0
+    def to_param
0
+      "#{id}-#{first_name.downcase}-#{last_name.downcase}"
0
+    end
0
   end
0
   
0
   class UsersController < ApplicationController
0
-    # GET /users/1
0
+    # GET /users/1-bobby-mcfarin
0
     def show
0
-      @user = User.from_param(params[:id]) # => <User id=1 login=mbleigh>
0
+      @user = User.from_param(params[:id]) # => <User id=1 first_name="Bobby" last_name="McFarin">
0
     end
0
   end
0
   
0
-  # overriding
0
+  # using a 'param' column, in this case 'slug'
0
   
0
-  class User < ActiveRecord::Base
0
+  class Post < ActiveRecord::Base
0
+    set_param_column "slug" # defaults to "param"
0
     def to_param
0
-      login
0
-    end
0
-    
0
-    def self.from_param(parameter)
0
-      User.find_by_login(parameter)
0
+      "#{created_at.strftime("%Y-%m-%d")}-#{title.gsub(" ","-").downcase.gsub(/[^a-z0-9-]/,"")}"
0
     end
0
   end
0
   
0
-  class UsersController < ApplicationController
0
-    # GET /users/mbleigh
0
-    @user = User.from_param(params[:id]) # => <User id=1 login=mbleigh>
0
+  class PostsController < ApplicationController
0
+    # GET /posts/2008-04-26-from-param-plugin-released
0
+    @post = Post.from_param(params[:id]) # => <Post title="From Param: Plugin Released" created_at="2008-04-26">
0
   end
0
+  
0
+Resources
0
+=========
0
+
0
+Lighthouse: 
0
 
0
 Copyright (c) 2008 Michael Bleigh, released under the MIT license
...
1
2
3
 
4
...
 
 
1
2
3
0
@@ -1,2 +1 @@
0
-require 'from_param'
0
-ActiveRecord::Base.send :include, FromParam
0
\ No newline at end of file
0
+require 'from_param'
0
\ No newline at end of file
...
1
2
3
4
5
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
8
 
 
 
 
 
9
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
12
...
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
0
@@ -1,11 +1,52 @@
0
-module FromParam
0
-  def self.included(base)
0
-    base.extend ClassMethods
0
-  end
0
-  
0
-  module ClassMethods
0
+class ActiveRecord::Base
0
+  # Class Methods
0
+  class << self
0
+    # The column that is currently set to be used with
0
+    # the from_param method.
0
+    def param_column
0
+      "param"
0
+    end
0
+    alias :param_column= :param_column
0
+    
0
+    # Allows you to set the column that will be used
0
+    # by from_param by default.
0
+    def set_param_column(val)
0
+      define_attr_method :param_column, val
0
+    end
0
+
0
+    # Returns true if the param_column is one of the
0
+    # currently defined columns of your table.
0
+    def param_column?
0
+      column_names.include?(param_column)
0
+    end
0
+    
0
+    # Takes a parameter generated by to_param and
0
+    # tried to find a matching record. If param_column
0
+    # is set and exists, uses that as the finder, otherwise
0
+    # uses the id such as in the default Rails to_param.
0
     def from_param(parameter)
0
-      find(parameter.to_i)
0
+      if param_column?
0
+        send "find_by_#{param_column}", parameter
0
+      else
0
+        find_by_id(parameter.to_i)        
0
+      end
0
     end
0
   end
0
+  
0
+  # Calls param_column? on the class
0
+  def param_column?
0
+    self.class.param_column?
0
+  end
0
+  
0
+  # Calls param_column on the class
0
+  def param_column
0
+    self.class.param_column
0
+  end
0
+  
0
+  before_save :set_param
0
+  # Automatically called before saving, sets
0
+  # the param_column to the current to_param
0
+  def set_param
0
+    send "#{param_column}=", to_param if param_column?
0
+  end
0
 end
0
\ No newline at end of file

Comments