How do you customize transliterations in a Rails 3 app? - ruby-on-rails-3

Ultimately, I would like to use Inflector.parameterize to create slugs for article heading that have a bunch of unicode chars in them (e.g. "ḤellẒ no" => "hellz-no"). According to http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-transliterate it says to put them in the locales/en.yml file.
# Store the transliterations in locales/en.yml
i18n:
transliterate:
rule:
Ḥ: "h"
Ẓ: "z"
I tried that but the following does not work:
"ḤellẒ no".parameterize
# => "ell-no"
However, when I change it in Ruby like the second paragraph suggests, it works.
I18n.backend.store_translations(:en, :i18n => {
:transliterate => {
:rule => {
"Ḥ" => "H",
"Ẓ" => "Z"
}
}
})
"ḤellẒ no".parameterize
# => "hellz-no"
I guess I would like to know why putting the custom transliterations in locales/en.yml doesn't work.
And even if someone give the answer for that, being a Rails noob, I would also like to know where one usually puts code like the second block to manually set the I18n.backend.store_translations?

Ehh, I've got a part of the answer. Unlike what the doc at http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-transliterate says, the yml files should still specify the language - i.e. de:
# Store the transliterations in locales/de.yml
de:
i18n:
transliterate:
rule:
ü: "ue"
ö: "oe"
Please still answer the second part of the question, where should code like I18n.backend.store_translations(:en,... live in a Rails 3 app?

[...] where should code like I18n.backend.store_translations(:en,... live in a Rails 3 app?
I know. I might be a little late on this but I would put it into an initializer file: config/initializers/i18n.rb

Related

Rails - Regex to remove accent from bank word

I'm new to Rails and I'm using Google Translate to post here. I have a doubt.
I have the following scope:
I'm trying to remove the accent from the bank words when performing a search.
In the parameters it already works with parameterize, but how do I query the bank (inside the where) to remove the accent?
I'm using postgresql.
Initially I tried to use regex_replace, but apparently it didn't work, could I be applying it wrong?
scope :filter_occupation, -> (params) {
params[:occupation].present? ?
where("lower(regexp_replace(occupation, '[^\w]+','')) LIKE ?",
# where("lower(occupation) LIKE ?",
"%#{params[:occupation].parameterize(separator: ' ')}%")
:
all
}

RoR Translation Interpolation - with values from other translations?

Say I have these translations:
en:
article:
name: "article"
Is it possible to do something like:
test1: "We have zero #article.name.pluralize"
test2: "There is an error in your #article.name"
test3: "Title for this page is: My #article.name.pluralize.capitalize"
I'm not too fussed about 1 and 3 (i.e. performing additional functions on the variable), but test2 would be a great help.
This also begs the question: is this a good way to structure translation files? I ask this because other languages can be structured in a completely different way. I'm just thinking of ways to DRY up my translation files.
You can possibly define a translation like this:
test1: "We have zero %{things}"
And then, somewhere in your code you can use it like this:
I18n.t(:test1, :scope => [:flashes], :things => t(:name, :scope => [:article] ))
or, using different notation:
I18n.t("flashes.test1", :things => I18n.t("article.name"))

Amazon API -- Can I search Category ALL - Other than DVD etc?

I Am trying to build play with API code from Amazon -- I am a noob at this --
I have created a product search using the simple lookup code, and have gone though and set the search field form a form submission works fine, how ever I don't want to set a category Like I am currently below to say DVD, BABY MUSIC, I wish to set to ALL is this possible?
include("amazon_api_class.php");
$obj = new AmazonProductAPI(); -- I have edited this and added ALL as a category in here
try
{
$result = $obj->searchProducts($query,
AmazonProductAPI::BABY, -- I can change this to DVD or MUSIC and it works but if i set to ALL i get errors?
"TITLE"); - tryed changing this to KEYWORD doesnt work!
}
catch(Exception $e)
Any Help Would Be nice.
Thanks
Carl
OK --- updated -- ANd I belive I have to use KEYWORD when USING ALL so I have added this in
case "KEYWORD" : $parameters = array("Operation" => "ItemSearch",
"Title" => $search,
"SearchIndex" => $category,
"ResponseGroup" => "Small",
"MerchantId" => "All",
"Condition"=>"New",
'Keywords' => $searchTerm);
Warning: Invalid argument supplied for foreach() in /data/ADMINwhere2shoponline/www/include/amazon.php on line 23
still get this error?
carl
Carl,
You should be able to use ALL as the search parameter, but you need to make sure that the number of ItemPage you are requesting is not more than 5 or it will return an error. All other categories allow up to 10, but ALL is limited to 5.
Check that and see if you yet your problem resolved.

Rails number_to_currency precision doesn't work - is it a bug?

I'm trying to use number_to_currency to get two digits after the decimal dot in Rails 3, but I can get only one. My DB field is defined like this:
t.decimal "amount", :precision => 5, :scale => 2
In my template I have:
#{number_to_currency(#purch.amount)}
And yet as the result I get: PLN 34,7
In the DB I can clearly see: 34.70
I would like to see my second zero - is it a feature or am I missing something?
Thanks!
The answer is to check the following setting in one's locale configuration file:
currency:
format:
strip_insignificant_zeros: false
Since you seem to be using a custom locale, try forcing the behavior you want by explicitly setting the options of number_to_currency:
number_to_currency(#purch.amount, :precision => 2)
You can also set these options in the locale file for your language. Take a look at http://guides.rubyonrails.org/i18n.html

Rails 3 ActiveRecord query using both SQL IN and SQL OR operators

I'm writing a Rails 3 ActiveRecord query using the "where" syntax, that uses both the SQL IN and the SQL OR operator and can't figure out how to use both of them together.
This code works (in my User model):
Question.where(:user_id => self.friends.ids)
#note: self.friends.ids returns an array of integers
but this code
Question.where(:user_id => self.friends.ids OR :target => self.friends.usernames)
returns this error
syntax error, unexpected tCONSTANT, expecting ')'
...user_id => self.friends.ids OR :target => self.friends.usern...
Any idea how to write this in Rails, or just what the raw SQL query should be?
You don't need to use raw SQL, just provide the pattern as a string, and add named parameters:
Question.where('user_id in (:ids) or target in (:usernames)',
:ids => self.friends.ids, :usernames => self.friends.usernames)
Or positional parameters:
Question.where('user_id in (?) or target in (?)',
self.friends.ids, self.friends.usernames)
You can also use the excellent Squeel gem, as #erroric pointed out on his answer (the my { } block is only needed if you need access to self or instance variables):
Question.where { user_id.in(my { self.friends.ids }) |
target.in(my { self.friends.usernames }) }
Though Rails 3 AR doesn't give you an or operator you can still achieve the same result without going all the way down to SQL and use Arel directly. By that I mean that you can do it like this:
t = Question.arel_table
Question.where(t[:user_id].in(self.friends.ids).or(t[:username].in(self.friends.usernames)))
Some might say it ain't so pretty, some might say it's pretty simply because it includes no SQL. Anyhow it most certainly could be prettier and there's a gem for it too: MetaWhere
For more info see this railscast: http://railscasts.com/episodes/215-advanced-queries-in-rails-3
and MetaWhere site: http://metautonomo.us/projects/metawhere/
UPDATE: Later Ryan Bates has made another railscast about metawhere and metasearch: http://railscasts.com/episodes/251-metawhere-metasearch
Later though Metawhere (and search) have become more or less legacy gems. I.e. they don't even work with Rails 3.1. The author felt they (Metawhere and search) needed drastic rewrite. So much that he actually went for a new gem all together. The successor of Metawhere is Squeel. Read more about the authors announcement here:
http://erniemiller.org/2011/08/31/rails-3-1-and-the-future-of-metawhere-and-metasearch/
and check out the project home page:
http://erniemiller.org/projects/squeel/
"Metasearch 2.0" is called Ransack and you can read something about it from here:
http://erniemiller.org/2011/04/01/ransack-the-library-formerly-known-as-metasearch-2-0/
Alternatively, you could use Squeel. To my eyes, it is simpler. You can accomplish both the IN (>>) and OR (|) operations using the following syntax:
Question.where{(:user_id >> my{friends.id}) | (:target >> my{friends.usernames})}
I generally wrap my conditions in (...) to ensure the appropriate order of operation - both the INs happen before the OR.
The my{...} block executes methods from the self context as defined before the Squeel call - in this case Question. Inside of the Squeel block, self refers to a Squeel object and not the Question object (see the Squeel Readme for more). You get around this by using the my{...} wrapper to restore the original context.
raw SQL
SELECT *
FROM table
WHERE user_id in (LIST OF friend.ids) OR target in (LIST OF friends.usernames)
with each list comma separate. I don't know the Rails ActiveRecord stuff that well. For AND you would just put a comma between those two conditions, but idk about OR