Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/from_param.rb
Class Method Summary collapse
-
.from_param(parameter) ⇒ Object
Takes a parameter generated by to_param and tried to find a matching record.
-
.param_column ⇒ Object
The column that is currently set to be used with the from_param method.
-
.param_column= ⇒ Object
The column that is currently set to be used with the from_param method.
-
.param_column? ⇒ Boolean
Returns true if the param_column is one of the currently defined columns of your table.
- .param_taken?(instance) ⇒ Boolean
-
.set_param_column(val) ⇒ Object
Allows you to set the column that will be used by from_param by default.
Instance Method Summary collapse
-
#param_column ⇒ Object
Calls param_column on the class.
-
#param_column? ⇒ Boolean
Calls param_column? on the class.
-
#set_param ⇒ Object
Automatically called before saving, sets the param_column to the current to_param.
Class Method Details
.from_param(parameter) ⇒ Object
Takes a parameter generated by to_param and tried to find a matching record. If param_column is set and exists, uses that as the finder, otherwise uses the id such as in the default Rails to_param.
27 28 29 30 31 32 33 |
# File 'lib/from_param.rb', line 27 def from_param(parameter) if param_column? send "find_by_#{param_column}", parameter else find_by_id(parameter.to_i) end end |
.param_column ⇒ Object
The column that is currently set to be used with the from_param method.
6 7 8 |
# File 'lib/from_param.rb', line 6 def param_column "param" end |
.param_column= ⇒ Object
The column that is currently set to be used with the from_param method.
9 10 11 |
# File 'lib/from_param.rb', line 9 def param_column "param" end |
.param_column? ⇒ Boolean
Returns true if the param_column is one of the currently defined columns of your table.
19 20 21 |
# File 'lib/from_param.rb', line 19 def param_column? column_names.include?(param_column) end |
.param_taken?(instance) ⇒ Boolean
65 66 67 68 69 70 71 |
# File 'lib/from_param.rb', line 65 def param_taken?(instance) return false if !param_column? conditions = instance.new_record? ? nil : ["id <> ?", instance.id] check_value = instance.send "#{param_column}" return true if instance.class.send("find_by_#{param_column}", check_value, :conditions => conditions) false end |
.set_param_column(val) ⇒ Object
Allows you to set the column that will be used by from_param by default.
13 14 15 |
# File 'lib/from_param.rb', line 13 def set_param_column(val) define_attr_method :param_column, val end |
Instance Method Details
#param_column ⇒ Object
Calls param_column on the class
42 43 44 |
# File 'lib/from_param.rb', line 42 def param_column self.class.param_column end |
#param_column? ⇒ Boolean
Calls param_column? on the class
37 38 39 |
# File 'lib/from_param.rb', line 37 def param_column? self.class.param_column? end |
#set_param ⇒ Object
Automatically called before saving, sets the param_column to the current to_param
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/from_param.rb', line 49 def set_param send "#{param_column}=", to_param if param_column? if self.class.param_taken?(self) i = 2 base_value = send "#{param_column}" send "#{param_column}=", "#{base_value}-#{i}" while(self.class.param_taken?(self)) # base_value = send "#{param_column}" # send "#{param_column}=", base_value.chop + (i+=1).to_s send "#{param_column}=", "#{base_value}-#{i+=1}" end end end |