Module: Railslove::Plugins::FindByParam::SingletonMethods

Defined in:
lib/find_by_param.rb

Instance Method Summary collapse

Instance Method Details

#find_by_param(value, args = {}) ⇒ Object

Search for an object by the defined permalink column. Similar to find_by_login. Returns nil if nothing is found. Accepts an options hash as second parameter which is passed on to the rails finder.


107
108
109
110
111
112
113
114
115
# File 'lib/find_by_param.rb', line 107

def find_by_param(value, args={})
  if permalink_options[:prepend_id]
    param = "id"
    value = value.to_i
  else
    param = permalink_options[:param]
  end
  self.send("find_by_#{param}".to_sym, value, args)
end

#find_by_param!(value, args = {}) ⇒ Object

Like find_by_param but raises an ActiveRecord::RecordNotFound error if nothing is found - similar to find().

Accepts an options hash as second parameter which is passed on to the rails finder.

Raises:

  • (::ActiveRecord::RecordNotFound)

125
126
127
128
129
130
# File 'lib/find_by_param.rb', line 125

def find_by_param!(value, args={})
  param = permalink_options[:param]
  obj = find_by_param(value, args)
  raise ::ActiveRecord::RecordNotFound unless obj
  obj
end