how can I define a specil struct by factory girl? - ruby-on-rails

Using RSpec and Factory Girl, I create a factory like this:
factory :hot_type do
ad_type_id 4
city_id 110000
image "111"
content { :link_url => 'xxx', :link_title => 'xxx', :count => 11 }
end
when I ran the rspec, it happened an error:
syntax error, unexpected ',', expecting '}'
content { :link_url => 'xx', :link_title => '1xx', :count => 11 }
How can I define the right struct?

Ruby interprets space + {} as a block. You need you specify the hash as arguments explicitly. Just use brackets around it.
content({ :link_url => 'xx', :link_title => '1xx', :count => 11 })

Related

How can I modify created between scope with lambda written in Rails 3.2 to Rails 4.2?

While migrating my application from Rails 3.2 to Rails 4.2, I have seen many changes, in that, scope syntax is one of the thing.
I modified all the other scopes but worried of this one:
scope :created_between, lambda { |start_at, end_at|
{ :conditions => {'users.created_at' => (start_at..end_at)} }
}
The above scope gives the users created between particular days and the related controller method code is:
def user_count
result = []
[
{:label => 'today', :start => Time.zone.now.beginning_of_day, :end => Time.zone.now.end_of_day},
{:label => 'yesterday', :start => 1.days.ago.beginning_of_day, :end => 1.days.ago.end_of_day},
{:label => 'this week', :start => Time.zone.now.beginning_of_week, :end => Time.zone.now.end_of_week},
{:label => 'last week', :start => 7.days.ago(Time.zone.now.beginning_of_week), :end => 7.days.ago(Time.zone.now.end_of_week)},
{:label => 'this month', :start => Time.zone.now.beginning_of_month, :end => Time.zone.now.end_of_month},
{:label => 'last month', :start => Time.zone.now.prev_month.beginning_of_month, :end => Time.zone.now.prev_month.end_of_month},
].each do |time_frame|
result << [time_frame[:label], User.includes(:account).only_deleted.deleted_between(time_frame[:start], time_frame[:end]).count, User.includes(:account).with_deleted.created_between(time_frame[:start], time_frame[:end]).count]
end
end
How to modify the above scope that matches the controller method with current Rails 4.2 syntax?
Rails 4.2 uses the shorter lambda style for scopes, so that will become something like:
scope :created_between, -> (start_at, end_at) {
where("? BETWEEN start_at AND end_at", 'users.created_at')
}

Split gem dashboard actions don't work

When trying to reset experiments in the Split gem for a/b testing, I keep getting server errors:
NoMethodError at /_split/reset/listing_headline
undefined method `reset' for nil:NilClass
file: dashboard.rb location: block in <class:Dashboard> line: 31
Any ideas?
Maybe not in your case, but I've seen this when someone was using symbols as test names instead of strings.
example: don't use:
:myTest => {}
use:
"myTest" => {
:alternatives => [
{ :name => 'default', :percent => 50 },
{ :name => 'alt', :percent => 50 },
],
:resettable => false
:metric => :lead,
}

'Encoding::UndefinedConversionError "\xE9" from ASCII-8BIT to UTF-8' when using some ruby gems that using file.open in ruby on rails

What's I want?
I want to generate .docx file or .odt file from template file in Rails 3.2
I want to use Japanese in it.
In ubuntu server 12.04 & ruby 1.9.3p194 & rails 3.2.8
What's happen?
I tried gems 'docx-templater' and 'serenity'
ruby-docx-templater
https://github.com/jawspeak/ruby-docx-templater
1 sample works good
2 try to do the same in my rails app
in controller as is sample
def gen_docx
input_file = './app/template/ExampleTemplate.docx'
data = {
:teacher => "Priya Vora",
:building => "Building #14",
:classroom => :'Rm 202',
:district => "Washington County Public Schools",
:senority => 12.25,
:roster => [
{:name => 'Sally', :age => 12, :attendence => '100%'},
{:name => :Xiao, :age => 10, :attendence => '94%'},
{:name => 'Bryan', :age => 13, :attendence => '100%'},
{:name => 'Larry', :age => 11, :attendence => '90%'},
{:name => 'Kumar', :age => 12, :attendence => '76%'},
{:name => 'Amber', :age => 11, :attendence => '100%'},
{:name => 'Isaiah', :age => 12, :attendence => '89%'},
{:name => 'Omar', :age => 12, :attendence => '99%'},
{:name => 'Xi', :age => 11, :attendence => '20%'},
{:name => 'Noushin', :age => 12, :attendence => '100%'}
],
:event_reports => [
{:name => 'Science Museum Field Trip', :notes => 'PTA sponsored event. Spoke to Astronaut with HAM radio.'},
{:name => 'Wilderness Center Retreat', :notes => '2 days hiking for charity:water fundraiser, $10,200 raised.'}
],
:created_at => "11-12-03 02:01"
}
DocxTemplater::DocxCreator.new(input_file, data).generate_docx_file()
end
3 but the error raised
the error raised at following point in gem (docx_templater.rb 22)
File.open(file_name, 'w') { |f| f.write(buffer) }
serenity
https://github.com/kremso/serenity
1 sample works well
2 do the same in my rails app and works well
like this
#encoding: utf-8
require 'serenity'
class Showcase
include Serenity::Generator
Person = Struct.new(:name, :items)
Item = Struct.new(:name, :usage)
def generate_showcase
#title = 'Serenity inventory'
mals_items = [Item.new('Moses Brothers Self-Defense Engine Frontier Model B', 'Lock and load')]
mal = Person.new('Malcolm Reynolds', mals_items)
jaynes_items = [Item.new('Vera', 'Callahan full-bore auto-lock with a customized trigger, double cartridge and thorough gauge'),
Item.new('Lux', 'Ratatata'),
Item.new('Knife', 'Cut-throat')]
jayne = Person.new('Jayne Cobb', jaynes_items)
#crew = [mal, jayne]
render_odt 'app/template/showcase.odt'
end
end
3 I tried my template including Japanese but the error raised.
the error raised at following point in gem(template.rb 22)
def process context
tmpfiles = []
Zip::ZipFile.open(#template) do |zipfile|
%w(content.xml styles.xml).each do |xml_file|
content = zipfile.read(xml_file)
odteruby = OdtEruby.new(XmlReader.new(content))
out = odteruby.evaluate(context)
tmpfiles << (file = Tempfile.new("serenity"))
file << out #!!!! HERE !!!!
file.close
zipfile.replace(xml_file, file.path)
end
end
end
What I did?
I found that 'serenity' have rspec test for Greek (UTF-8) in gem.I tried Japanese the same way. and the test passed!. so I thought the problem is not in gems in rails setting.
add magic comment "#encoding: utf-8" to my controller or lib file
confirm 'config.encoding = "utf-8"' in config/application.rb
add following to config/enviroment.rb above "initialize!"
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
I don't use any database in my rails app.
But all is nothing to do with my case... any idea?
I may not know the basic things ...
This is a little old, but I just ran into this same situation. In my case, it was a matter of setting the tempfile to binary mode prior to writing to it:
tmpfiles << (file = Tempfile.new("serenity"))
file.binmode
file << out
file.close
Hope this helps

rails functional testing issue

When I do
get :inside, :format => :xml, :lat2 => "41", :lng2 => "-73.9", :lat1 => "40", :lng1 => "-74", :category => "girl", :order => "date"
with my routes.rb includes:
get 'images/inside/:lat1/:lng1/:lat2/:lng2/:order/:category', :to => "images#inside"
I get
ActionController::RoutingError: No route matches {:lng1=>"-74", :category=>"girl", :lat2=>"41", :format=>:xml, :lng2=>"-73.9", :order=>"date", :lat1=>"40", :action=>"inside", :controller=>"images"}
But when I do
get :inside, :format => :xml, :lat2 => "41", :lng2 => "-73", :lat1 => "40", :lng1 => "-74", :category => "girl", :order => "date"
it works!
The only difference is the decimal value of lng2.
Note that if routes.rb has no params, it works, but I need those
As far as I can tell, it's because your value has a period. By default, Rails (at least Rails3) routes cannot contain periods. To work around this, see http://avdi.org/devblog/2010/06/18/rails-3-resource-routes-with-dots-or-how-to-make-a-ruby-developer-go-a-little-bit-insane/
I bet the decimal point is making Rails think that there's a format being specified (e.g. .xml or .js).
You might be able to get around it using a regex, something like what's described here: http://zargony.com/2009/05/05/routing-parameters-with-a-dot

Testing Paperclip file uploading with RSpec

I don't really care about testing file uploads, but since I have validates_attachment_presence, etc.. in my model, rspec is complaining.
So now I'm creating my model with these attributes in the spec to try and shut it up:
#attr = {
:name => "value for name",
:title => "value for title",
:content => "value for content",
:pic_file_name => "example.jpg",
:pic_content_type => "image/jpg",
:pic_file_size => "8192",
:pic_updated_at => nil
}
This doesn't work, though.
I found this: http://fr.ivolo.us/posts/mocking-paperclip-with-rspec
So I tried something like this:
Post.should_receive(:save_attached_files).and_return(true)
Which doesn't work either. How do I appease RSpec?
If the model has_attached_file :pic, you should be able to just point the pic attribute at some file and all should be dandy.
Meaning something like #attr = { :pic => File.open(File.join(Rails.root, 'spec', 'fixtures', 'file.png')) }

Resources