URL encoded route to Rails controller - ruby-on-rails

I have a URL encoded resource such as:
http://myurl/users/Joe%20Bloggs/index.xml
This is for a RESTful webservice which uses user logins in the path. The problem is that the controller in rails doesn't seem to decode the %20. I get the following error:
ActionController::RoutingError (No route matches "/Joe%20Bloggs/index.xml" with {:method=>:post}):
What I'm actually trying to do is achieve one of 2 options (using authlogic as my registrations handler):
Either (preferably) allow users to register user names with spaces in them, and have these get routed correctly to my controller. Authlogic by default allows spaces & #/. characters - which is just fine with me if I can make it work...
Or I can restrict authlogic to dissallow the spaces. I know I can do this with:
.merge_validates_format_of_login_field_options...
but I'm not entirely sure of the correct syntax to provide the new regex and return message on failure...
Any suggestions greatly appreciated!

Generally it's a better idea to have a URL-safe "slug" field in your models for situations like this. For example:
class User < ActiveRecord::Base
before_validation :assign_slug
def to_param
# Can't use alias_method on methods not already defined,
# ActiveRecord creates accessors after DB is connected.
self.slug
end
def unique_slug?
return false if (self.slug.blank?)
if (new_record?)
return self.class.count(:conditions => [ 'slug=?', self.slug ]) == 0
else
return self.class.count(:conditions => [ 'slug=? AND id!=?', self.slug, self.id ]) == 0
end
end
def assign_slug
return if (slug.present?)
base_slug = self.name.gsub(/\s+/, '-').gsub(/[^\w\-]/, '')
self.slug = base_slug
count = 1
# Hunt for a unique slug starting with slug1 .. slugNNN
while (!unique_slug?)
self.slug = base_slug + count.to_s
count += 1
end
end
end
This may solve the problem of having non-URL-friendly names. Rails is particularly ornery when it comes to having dots in the output of to_param.

Related

Rails how to use where method for search or return all?

I am trying to do a search with multiple attributes for Address at my Rails API.
I want to search by state, city and/or street. But user doesn't need to send all attributes, he can search only by city if he wants.
So I need something like this: if the condition exists search by condition or return all results of this condition.
Example:
search request: street = 'some street', city = '', state = ''
How can I use rails where method to return all if some condition is nil?
I was trying something like this, but I know that ||:all doesn't work, it's just to illustrate what I have in mind.:
def get_address
address = Adress.where(
state: params[:state] || :all,
city: params[:city] || :all,
street: params[:street] || :all)
end
It's possible to do something like that? Or maybe there is a better way to do it?
This is a more elegant solution using some simple hash manipulation:
def filter_addesses(scope = Adress.all)
# slice takes only the keys we want
# compact removes nil values
filters = params.permit(:state, :city, :street).to_h.compact
scope = scope.where(filters) if filters.any?
scope
end
Once you're passing a column to where, there isn't an option that means "on second thought don't filter by this". Instead, you can construct the relation progressively:
def get_address
addresses = Address.all
addresses = addresses.where(state: params[:state]) if params[:state]
addresses = addresses.where(city: params[:city]) if params[:city]
addresses = addresses.where(street: params[:street]) if params[:street]
addresses
end
I highly recommend using the Searchlight gem. It solves precisely the problem you're describing. Instead of cluttering up your controllers, pass your search params to a Searchlight class. This will DRY up your code and keep your controllers skinny too. You'll not only solve your problem, but you'll have more maintainable code too. Win-win!
So in your case, you'd make an AddressSearch class:
class AddressSearch < Searchlight::Search
# This is the starting point for any chaining we do, and it's what
# will be returned if no search options are passed.
# In this case, it's an ActiveRecord model.
def base_query
Address.all # or `.scoped` for ActiveRecord 3
end
# A search method.
def search_state
query.where(state: options[:state])
end
# Another search method.
def search_city
query.where(city: options[:city])
end
# Another search method.
def search_street
query.where(street: options[:street])
end
end
Then in your controller you just need to search by passing in your search params into the class above:
AddressSearch.new(params).results
One nice thing about this gem is that any extraneous parameters will be scrubbed automatically by Searchlight. Only the State, City, and Street params will be used.

Rails 5 - iterate until field matches regex

In my app that I am building to learn Rails and Ruby, I have below iteration/loop which is not functioning as it should.
What am I trying to achieve?
I am trying to find the business partner (within only the active once (uses a scope)) where the value of the field business_partner.bank_account is contained in the field self_extracted_data and then set the business partner found as self.sender (self here is a Document).
So once a match is found, I want to end the loop. A case exists where no match is found and sender = nil so a user needs to set it manually.
What happens now, is that on which ever record of the object I save (it is called as a callback before_save), it uses the last identified business partner as sender and the method does not execute again.
Current code:
def set_sender
BusinessPartner.active.where.not(id: self.receiver_id).each do |business_partner|
bp_bank_account = business_partner.bank_account.gsub(/\s+/, '')
rgx = /(?<!\w)(#{Regexp.escape(bp_bank_account)})?(?!\‌​w)/
if self.extracted_data.gsub(/\s+/, '') =~ rgx
self.sender = business_partner
else
self.sender = nil
end
end
end
Thanks for helping me understand how to do this kind of case.
p.s. have the pickaxe book here yet this is so much that some help / guidance would be great. The regex works.
Using feedback from #moveson, this code works:
def match_with_extracted_data?(rgx_to_match)
extracted_data.gsub(/\s+/, '') =~ rgx_to_match
end
def set_sender
self.sender_id = matching_business_partner.try(:id) #unless self.sender.id.present? # Returns nil if no matching_business_partner exists
end
def matching_business_partner
BusinessPartner.active.excluding_receiver(receiver_id).find { |business_partner| sender_matches?(business_partner) }
end
def sender_matches?(business_partner)
rgx_registrations = /(#{Regexp.escape(business_partner.bank_account.gsub(/\s+/, ''))})|(#{Regexp.escape(business_partner.registration.gsub(/\s+/, ''))})|(#{Regexp.escape(business_partner.vat_id.gsub(/\s+/, ''))})/
match_with_extracted_data?(rgx_registrations)
end
In Ruby you generally want to avoid loops and #each and long, procedural methods in favor of Enumerable iterators like #map, #find, and #select, and short, descriptive methods that each do a single job. Without knowing more about your project I can't be sure exactly what will work, but I think you want something like this:
# /models/document.rb
class Document < ActiveRecord::Base
def set_sender
self.sender = matching_business_partner.try(:id) || BusinessPartner.active.default.id
end
def matching_business_partners
other_business_partners.select { |business_partner| account_matches?(business_partner) }
end
def matching_business_partner
matching_business_partners.first
end
def other_business_partners
BusinessPartner.excluding_receiver_id(receiver_id)
end
def account_matches?(business_partner)
rgx = /(?<!\w)(#{Regexp.escape(business_partner.stripped_bank_account)})?(?!\‌​w)/
data_matches_bank_account?(rgx)
end
def data_matches_bank_account?(rgx)
extracted_data.gsub(/\s+/, '') =~ rgx
end
end
# /models/business_partner.rb
class BusinessPartner < ActiveRecord::Base
scope :excluding_receiver_id, -> (receiver_id) { where.not(id: receiver_id) }
def stripped_bank_account
bank_account.gsub(/\s+/, '')
end
end
Note that I am assigning an integer id, rather than an ActiveRecord object, to self.sender. I think that's what you want.
I didn't try to mess with the database relations here, but it does seem like Document could include a belongs_to :business_partner, which would give you the benefit of Rails methods to help you find one from the other.
EDIT: Added Document#matching_business_partners method and changed Document#set_sender method to return nil if no matching_business_partner exists.
EDIT: Added BusinessPartner.active.default.id as the return value if no matching_business_partner exists.

How to create case(switch) statement with a hash of constants using ruby on rails?

I want to create the equivalent of this code, except using a hash and some meta programming.
def current_verb
case params[:controller]
when "apps"
#current_verb = "MADE "
when "articles"
#current_verb = "LEARNED "
when "articles"
#current_verb = "WONDERED " # Change later to specify articles with a certain tag.
else
#current_verb = "IS "
end
end
Intuitively, I tried something like this, but it seems to be invalid.
So far I have this in the application_controller.rb
class ApplicationController < ActionController::Base
before_action :current_verb
private
verbs = { pages: "IS",
apps: "MADE",
articles: "TAUGHT",
articles: "WONDERED"
}
def current_verb
case params[:controller]
verbs.each |key, value| do
# need this to spit out literal code, not evaluate code
when key
#current_verb = value
end
else
#current_verb = verbs[:pages]
end
end
The tricky part about this is that I don't think I can use define_method or send because the looped portion isn't the entirety of the method. Thanks for the help.
It looks like you're just looking up a key in a hash and returning its value. The only caveat appears to be that if the ke isn't found then you want to return a default value of "IS". I would suggest using Hash#fetch for this.
class ApplicationController < ActionController::Base
VERBS = { pages: "IS",
apps: "MADE",
articles: "TAUGHT",
articles: "WONDERED"
}.stringify_keys.freeze
before_action :set_current_verb
private
def set_current_verb
#current_verb = VERBS.fetch(controller_name) { "IS" }
end
end
Note that I made VERBS a constant so that it would be visible to the set_current_verb method. And I included it above the private designation because constants can't be private anyway. Using the VERBS hash inside of the set_current_verb method would cause it to be evaluated every single time, so the constant is a better solution still. Also, controller_name is preferred over params[:controller] for its expressiveness and because it will avoid returning namespaces should any exist in the future.
Also, the VERBS hash seems to have two identical keys. I assume that's just a typo and can be corrected by you.

Quora-like duplicated slugs

I've looked on SO for an answer to this question, but can't seem to find one.
Basically what I want to do is something like Quora's Url Structure, where if a profile has the name Thomas Jefferson it becomes quora.com/thomas-jefferson. If there is already a Thomas Jefferson on the site, then it would become quora.com/thomas-jefferson-1, and so on for x number of duplicates.
The FriendlyId gem has something sort of like this, but instead of incrementing they generate a SecureRandom string, which is kind of ugly.
I have a Rails model that looks like this so far:
class Profile < ActiveRecord::Base
before_create :generate_slug
def generate_slug do
self.slug = loop do
slug = to_slug(self.name)
break slug unless Profile.exists?(slug: slug)
end
end
def to_slug(name)
self.transliterate.downcase.gsub(/[^a-z0-9 ]/, ' ').strip.gsub(/[ ]+/, '-')
end
end
I am assuming you have an index on name column in profiles table.
You can fire a query to database to get all entries like the currently generated slug and get the max value. If database does not return anything use current slug else parse the integer part of the max slug and increment it with 1 to get a new slug.
def generate_slug
slug = to_slug(self.name)
max_slug = Profile.where("slug like '#{slug}-%'").max.try(:slug)
self.slug = max_slug.present? ? slug : compute(slug, max_slug)
end
def compute(slug, max_slug)
max_count = max_slug.gsub("#{slug}-", "").to_i + 1
"#{slug}-#{max_count}"
end
*Untested code

Colons as divider with Rails Route?

I would like to use colons as divider in my rails route url instead of forward slashes. Is it possible to do the this?
I'm after something like the following
match '*page_path/:title ":" *section_path ":" :section_title' => 'pages#show'
so for the url food/fruit/apples:cooking:pie:apple_pie would return the parameters:
:page_path = "food/fruit"
:title = "apples"
:section_path = "cooking:pie"
:section_title = "apple_pie"
Is this possible in rails?
Here's an approach :
match 'food/fruit/:id' => 'pages#show' # add constraints on id if you need
class Recipe < ActiveRecord::Base
def self.from_param( param )
title, section_path, section_title = extract_from_param( param )
# your find logic here, ex: find_by_title_and_section_path_and_section_title...
end
def to_param
# build your param string here,
# ex: "#{title}:#{section_path}:#{section_title}"
# Beware ! now all your urls relative to this resource
# will use this method instead of #id.
# The generated param should be unique, of course.
end
private
def self.extract_from_param( param )
# extract tokens from params here
end
end
then in your controller:
#recipe = Recipe.from_param( params[:id] )
note that the use of use the built-in to_param method is optionnal.

Resources