factorygirl order of parameters break create() method? - ruby-on-rails

Why does:
fi = FactoryGirl.create(:finder_item, store_id: s.id, :category_foo, :random_question)
throw an error finder_item_spec.rb:20: syntax error, unexpected ',', expecting tASSOC (SyntaxError)
but simply re-ordering so the the traits are before the assignment works fine:
fi = FactoryGirl.create(:finder_item, :category_foo, :random_question, store_id: s.id)

One of your arguments is not just symbol type - it's :key => value, and for a FG order of arguments with different types is something that matters.

Related

setting conditional attributes for API connection

A method needs to instantiate a session with various attributes, some of which are optional
session = Checkout::Session.create({
locale: I18n.locale,
reference_id: id,
customer_email: #user_mail,
[...]
})
The last shown attribute, customer_email, is optional but it should not be generated if the value does not exist.
customer_email: #user_mail unless !#user_email,
logically hits a syntax error because an additional param (the comma) is being produced
syntax error, unexpected ',', expecting end
and thus the API expects another attribute.
(customer_email: #user_mail, unless !#user_email)
also fails as there is confusion over the parenthesis
syntax error, unexpected ')', expecting then or ';' or '\n'
How should this syntax be cast?
You need to extract the options hash into a variable and manipulate it before sending it to the Checkout::Session.create.
Something like this:
options = {
locale: I18n.locale,
reference_id: id
}
options[:customer_email] = #user_mail if #user_email
session = Checkout::Session.create(options)

Rails scope syntax issue

I am trying to define a scope whereby a field in a related object has a value of 1. It seems to throw an error when I do this:
scope :in_progress, ->{Submission.where(current_agent.agent_activities.last.Status: 1)}
Desired effect is to call all Submissions that have a status of "In progress" which has a hash value of 1.
Error:
SyntaxError (/Users/gbade/Desktop/RoR/Ottom8/app/models/submission.rb:16: syntax error, unexpected ':'
.agent_activities.last.Status: 1)}
^):

writing a rspec test for checking a function that returns a hash table

Trying to write an RSpec test that checks a function location_counts(piece) that returns a hash table with some keys and values assigned.
describe "location_counts(piece)" do
it "should return a hash table with a key of locations and value of times visited" do
game = FactoryGirl.create(:game)
black_queen = FactoryGirl.create(:queen, game_id: game.id, row: 8, column: 4, is_black: true)
black_queen.move_to(4,4)
black_queen.move_to(1,1)
black_queen.move_to(4,4)
expect(game.location_counts(black_queen)).to be {"4, 4"=>2, "1, 1"=>1}
end
end
With this test, I'm getting a syntax error.
That looks like:
/usr/local/rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `load': /home/ubuntu/workspace/chess-app/spec/models/game_spec.rb:101: syntax error, unexpected =>, expecting '}' (SyntaxError)
...s(black_queen)).to be {"4, 4"=>2, "1, 1"=>1}
... ^
/home/ubuntu/workspace/chess-app/spec/models/game_spec.rb:101: syntax error, unexpected =>, expecting '}'
...en)).to be {"4, 4"=>2, "1, 1"=>1}
... ^
The problem seems to be that ruby is interpreting your expected hash:
expect(game.location_counts(black_queen)).to be {"4, 4"=>2, "1, 1"=>1}
as a block passed to the be method, instead of as a parameter. Try the following:
expect(game.location_counts(black_queen)).to eq({"4, 4"=>2, "1, 1"=>1})
or even removing the curly braces, as the hash is the last parameter passed to the be method:
expect(game.location_counts(black_queen)).to eq("4, 4"=>2, "1, 1"=>1)
EDIT: Regard the usage of eq instead of be.

Having trouble using where.not in rails

I am trying to use where.not to replace the following:
if #friend_matches.count > 0
#court_matches = Match.available_on_courts.where('matches.id NOT IN (?)', #friend_matches.pluck(:id)).to_a
else
#court_matches = Match.available_on_courts
end
With
#court_matches = Match.available_on_courts.where.not(matches.id: #friend_matches.pluck(:id)).to_a
However I am getting the following errors.
SyntaxError: /Users/sripaladugu/Coding/matchpoint_rails/app/mailers/match_mailer.rb:8: syntax error, unexpected ':'
...on_courts.where.not(matches.id: #friend_matches.pluck(:id))....
... ^
/Users/sripaladugu/Coding/matchpoint_rails/app/mailers/match_mailer.rb:8: syntax error, unexpected ')', expecting keyword_end
...id: #friend_matches.pluck(:id)).to_a
You can provide a hash within where to specify table names as keys and column names within the second level:
#court_matches = Match.available_on_courts
.where.not(matches: { id: #friend_matches.pluck(:id) })
.to_a

formatting JSON object in rails 3

I have a rails app has an api for iphone/android clients. I'm doing Message.find(1).to_json(:include => :user) but that adds \ around " to escape it.
How can I prevent to_json from escaping the double quotes?
>> { "a" => "blah" }.to_json
=> "{\"a\":\"blah\"}"
I would like it to be { "a" : "blah" } or { a : "blah" }
It looks like this is actually what you want.
What you are seeing is the string formatted for display (and human readability). It is delimited by double quotes, so the double quotes inside the string are escaped. In reality, the string contains double quotes, but for literal representation, they are escaped. If you stick this into a JSON parser you will find it returns the object you want.
If you were to print this out, you will find that you get the format you want.
irb(main):001:0> puts { "a" => "blah" }.to_json
{"a":"blah"}
=> nil
For further illustration, you could try parsing it. The string you ended up with returns your original object, because JSON is represented by a string. However, attempting to insert the desired content will give you a nasty syntax error or a TypeError. This is because JSON is not a literal in Ruby, whereas in JavaScript it can be used as a literal object. In Ruby it is a representation in the form of a string.
irb(main):002:0> JSON.parse("{\"a\":\"blah\"}")
=> {"a"=>"blah"}
irb(main):003:0> JSON.parse({ "a" : "blah" })
SyntaxError: (irb):3: syntax error, unexpected ':', expecting tASSOC
JSON.parse({ "a" : "blah" })
^
(irb):3: syntax error, unexpected '}', expecting $end
JSON.parse({ "a" : "blah" })
^
irb(main):004:0> JSON.parse({a:"blah"})
TypeError: can't convert Hash into String

Resources