Rails 3 - Custom JSON with Conditions - ruby-on-rails

I'm trying to DRY up my code and I have two methods that do almost the same thing but add an extra fields for one instance. It is formatting JSON in Rails and trying to see if there is a way where I can perform an if statement within the result.collect similar to below. Any help is much appreciated
results.collect{ |result| { :type => defined?(result.type) ? result.type : "Total",
:jan => result.jan, :feb => result.feb, :mar => result.mar, :apr => result.apr,
:may => result.may, :jun => result.jun, :jul => result.jul, :aug => result.aug,
:sep => result.sep, :oct => result.oct, :nov => result.nov, :dec => result.dec } }
in this statement
:type => defined?(result.type) ? result.type : "Total"
I want to be able perform a check if result.type exists in the query then list it else just put "Total"
Any ideas how to perform this? Thanks everyone

I ended up writing in the method to check if with_type or not. If true then I added the type select statement in and if false then added
Select 'Total' as Type
I was hoping to figure out how to modify the json as in the question but modifying the composed_scope worked too.

Related

FluentValidation properties conditions

I have two properties that must be validated, but once at a time. If one is null then validate the other, so I was trying like this :
RuleFor(a => a.CNPJ).Must(a => CNPHelper.CheckCNPJ(a)).When(a => !string.IsNullOrEmpty(a.CPF));
RuleFor(a => a.CPF).Must(a => CNPHelper.CheckCPF(a)).When(a => !string.IsNullOrEmpty(a.CNPJ));
But the must is mandatory, how to change it ?
Can you please try DependentRules extension.
As from docs (https://fluentvalidation.net/start#conditions) reference, you can achieve it as
RuleFor(x => x.Surname).NotNull().DependentRules(() => {
RuleFor(x => x.Forename).NotNull();
});

if condition in haml view

I want to set a :data attr if a condition is met. In this case a user should have a role. So it's a boolean statement. Let me give an example.
- #data = 'contract-confirm'
.create_button= link_to 'Something', new_some_path(#customer), :class => 'btn btn-success', :'data' => #data ? 'contract' : nil
.clearer
So I know this might look strange but I want to set a data attribute if customer is labeled and then hook js to that data attr. That part works. What does not work is that now I'm setting the attribute always. So even in the case that customer does not have the role the js gets hooked. I know that I am not explicitly indicating at all the role here. I have a #customer.role? but I cant seem to incorporate it properply. I managed it before with an if else statement but then with a lot of duplication which I'm not so fund of. Any tips?
You can try this piece of code:
link_to 'Something', ... , :data => #customer.role? ? 'contract' : nil
haml won't include nil attributes, so it should work as you expect.
Try to replace :'data' => #data ? 'contract' : nil with :'data' => 'contract' if #data.
I checked it and next code:
- #data_present = true
= link_to 'Something', root_path, :class => 'btn', :'data' => ('test' if #data_present)
renders to:
Something
And code without - #data_present = true renders to:
Something

select tag not honoring name field - RoR

Do you have any idea why the name (="language") here is not being honored? I expect it to be in the search query.. but instead it spits out something ugly like "company%5Blanguage%=".
<%=select( :company,:language,{'Italian' => 'Italian', 'English' => 'English','Italian and English' => 'Italian and English'},{:name => "language", :prompt=>true} )
I have tried to take it out of the brackets by the way.. no luck.
Thanks!
Ah interesting. In case this will help someone, here is my solution
select( :company,:language,{'Italian' => 'Italian',..},
{:name => "language", :prompt=>true},{:name =>
"language"}
The reason is that the form of the select tag is
select(object, method, choices, options = {}, html_options = {})
and you need to make sure the :name is set in html_options.
Thanks!

Rails -- Find condition with id's all over the place

I have this find condition pulling from my model that currently looks like this.
#major_set = Interest.find(:all, :conditions => {:id => 1..21})
I'd like to add some more individual ids that I've just added which are like 120...130. I tried to do...
#major_set = Interest.find(:all, :conditions => {:id => 1..21, 120..130})
but got the error. " syntax error, unexpected '}', expecting tASSOC ...ns => {:id => 1..21, 122..130})"
How can I select multiple sets of id's as well as maybe some individual ids, ie ( :conditions => {:id => 1..21, 121..140, 144, 155 }??
You can convert the ranges to arrays and add them together. E.g.
#major_set = Interest.find(:all,
:conditions => {:id => (1..21).to_a + (120..130).to_a})
If you then want to add in individual ids then you can just add them to the array. E.g.
ids_to_find = (1..21).to_a + (120..140).to_a
ids_to_find << 144
ids_to_find << 145
#major_set = Interest.find(:all, :conditions => { :id => ids_to_find })
If you type {:id => 1..21, 122..130} inside irb you will get an error because it is not valid ruby syntax. This is interpreted as a hash where the first element is :id => 1..21 and the second is missing its key.
In order to make it a valid ruby expression, you would need to type:
{:id=>[1..21, 122..130]}
But I don't think that ActiveRecord will accept that syntax. So you may need to:
:conditions => "id BETWEEN 1 AND 21 OR id BETWEEN 122 AND 130"
This works in MySQL. I don't know if it will work in other databases.

Ruby on Rails / Yellow Maps For Ruby Plugin woes

Okay I've read through the plugin comments and the docs as well and I have yet to come up with an answer as to how to do this. Here's my problem I want to use the :info_window_tabs and the :icon option, but I don't know what format to pass my information in. According to the documentation the following code should be correct. Here's my code:
#mapper.overlay_init(GMarker.new([map.lat, map.lng],
:title => map.name,
:info_window_tabs => [
{:tab => "HTML", :content => #marker_html},
{:tab => "Attachments", :content => "stuff"}],
:icon => {
:image => "../images/icon.png"
}))
The readme and documentation can be viewed here.
And the relevant ruby file that I am trying to interact with, including the author's comments, can be viewed here.
I have tried the #rubyonrails channel in IRC as well as emailing the author directly and reporting an issue at GitHub. It really is just a question of syntax.
Thanks!
Okay, so I finally got this figured out. Here's how you do it; :icon accepts a GIcon variable and :info_window_tabs accepts an array of GInfoWindowTabs. Here is how you would declare each with the plugin.
Declare GIcon
#mapper.icon_global_init(GIcon.new(:image => "../images/civil.png",
:icon_anchor => GPoint.new(0,0),
:shadow => "../images/shadow.png",
:shadow_size => GSize.new(37,32),
:info_window_anchor => GPoint.new(9,2)), "civil_icon")
#civil_icon = Variable.new("civil_icon")
Declare GInfoWindowTab
#tab1 = GInfoWindowTab.new('Tab 1 Label', 'HTML for inside of tab1')
#tab2 = GInfoWindowTab.new('Tab 2 Label', 'HTML for inside of tab2')
#window_tabs = [#tab1, #tab2]
Then in your GMarker declaration just do the following:
#mapper.overlay_init(GMarker.new([map.lat, map.lng],
:title => map.name,
:icon => #civil_icon,
:max_width => 300,
:info_window_tabs => #window_tabs))
And you're done.

Resources