Module: Railslove::Plugins::FindByParam::ClassMethods

Defined in:
lib/find_by_param.rb

Instance Method Summary collapse

Instance Method Details

This method initializes find_by_param

class Post < ActiveRecord::Base
  make_permalink :with => :title, :prepend_id => true
end

The only required parameter, is :with.

If you want to use a non URL-save attribute as permalink your model should have a permalink-column to save the escaped permalink value. This field is then used for search.

If your you can just say make_permalink :with => :login and you’re done.

You can use for example User.find_by_param(params, args) to find the user by the defined permalink.

Available options

:with

(required) The attribute that should be used as permalink

:field

The name of your permalink column. make_permalink first checks if there is a column, default is ‘permalink’.

:prepend_id
true|false

Do you want to prepend the ID to the permalink? for URLs like: posts/123-my-post-title - find_by_param uses the ID column to search, default is false.

:param_size
Number

Desired maximum size of the permalink, default is 50.

:escape
true|false

Do you want to escape the permalink value? (strip chars like öä?&) - actually you must do that, default is true.

:validate
true|false

Don’t validate the :with field - set this to false if you validate it on your own, default is true.

:forbidden
Regexp|String|Array of Strings

Define which values should be forbidden. This is useful when combining user defined values to generate permalinks in combination with restful routing. Make sure, especially in the case of a Regexp argument, that values may become valid by adding or incrementing a trailing integer.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/find_by_param.rb', line 65

def make_permalink(options={})
  options[:field] ||= "permalink"
  options[:param] = options[:with] # :with => :login - but if we have a spcific permalink column we need to set :param to the name of that column
  options[:escape] ||= true
  options[:prepend_id] ||= false
  options[:param_size] ||= 50
  options[:validate] = true if options[:validate].nil?

  # validate if there is something we can use as param. you can overwrite the validate_param_is_not_blank method to customize the validation and the error messge.
  if !options[:prepend_id] || !options[:validate]
    validate :validate_param_is_not_blank
  end

  if forbidden = options.delete(:forbidden)
    if forbidden.is_a? Regexp
      options[:forbidden_match] = forbidden
    else
      options[:forbidden_strings] = Array(forbidden).map(&:to_s)
    end
  end

  if self.column_names.include?(options[:field].to_s)
    options[:param] = options[:field]
    before_save :save_permalink
  end

  self.permalink_options = options
  extend Railslove::Plugins::FindByParam::SingletonMethods
  include Railslove::Plugins::FindByParam::InstanceMethods
rescue ActiveRecord::ActiveRecordError
  puts "[find_by_param error] database not available?"
end