Showing posts with label rails. Show all posts
Showing posts with label rails. Show all posts

Sunday, August 15, 2010

Factory Girl in Development

Some many developers use Factory Girl as a replacement to Fixtures and they depend on this gem in Test cases writing.

But Factory Girl can be very useful also in development and can be used in order to generate a bunch of dummy data with different specs for showing your work and reviewing all its details.

The problem we face is that Factory Girl can generate more records in tables that should have their data unchanged such as Countries table.

For example:

Factory.define :user do |f|
  f.association :country
end

Factory.define :job do |f|
  …
  f.association :user
end

Now, when we run this piece of code that generates 10 jobs

10.times { Factory.create(:job) }

we will get 10 countries auto-generated
violating the rule we wish to maintain which is having the countries table as it is

To workaround this problem without causing any changes in the code written previously, i came up with that solution

class Factory
  class << self
    alias_method :create_original, :create
    
    def create(name, overrides = {})
      if name.to_s == 'country'
        country = Country.first
        return country if country
      end

      create_original(name, overrides)
    end
  end
end

The logic introduced in the above script is simply adding a layer before creation that checks if the created object of certain type and if that type is desired to not be generated, we return the first entry we have in the DB else we do the normal creation

You can add this script in a file and load it in the development environment and use Factory Girl safely without fearing of generating data for tables that should remain as lookup tables

Enjoy :)

Monday, August 9, 2010

Factory Girl & Polymorphic Associations

Suppose you have a case like that

class Address < ActiveRecord::Base
  belongs_to :addressable, polymorphic => true
end

class Listing < ActiveRecord::Base
  has_one :address, as => :addressable
end

class Customer < ActiveRecord::Base
  has_one :address, as => :addressable
end

Now in your factories you will have something like that

Factory.define :listing do |f|
  f.association :address
end

since addressable is required, we choose it to be by default related to a customer unless else stated

Factory.define :address do |f|
  f.association :addressable, :factory => :customer
end

right now if you tried to use the Listing Factory, you will get an address associated with dummy customer and no effect on your side

How can this be fixed ?

Factory.define :listing do |f|
  f.after_build do |listing|
    listing.address = Factory.create(:address, :addressable => listing)
  end
end

This was the only solution i found after searching for a while
which is a good solution and doesn't need a lot of work to be done

Monday, August 31, 2009

smtp 555 5.5.2 Syntax error (Net::SMTPFatalError)

This title was part of an error message i got while dealing with action_mailer

i looked everywhere for a reason for this error message and found so many reasons but none of them matched with my case

So i liked to share why i got this error. Simply because i was adding no recipients to my email. I know it is very weird to send an email without having a recipient but getting this error message i much more weird and strange

/usr/lib/ruby/1.8/net/smtp.rb:930:in `check_response': 555 5.5.2 Syntax error. 24sm123817eyx.21 (Net::SMTPFatalError)
        from /usr/lib/ruby/1.8/net/smtp.rb:899:in `getok'
        from /usr/lib/ruby/1.8/net/smtp.rb:842:in `rcptto'
        from /usr/lib/ruby/1.8/net/smtp.rb:834:in `rcptto_list'
        from /usr/lib/ruby/1.8/net/smtp.rb:833:in `each'
        from /usr/lib/ruby/1.8/net/smtp.rb:833:in `rcptto_list'
        from /usr/lib/ruby/1.8/net/smtp.rb:654:in `sendmail'
        from /usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/base.rb:683:in `perform_delivery_smtp'
        from /usr/lib/ruby/1.8/net/smtp.rb:526:in `start'
        from /usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/base.rb:681:in `perform_delivery_smtp'
        from /usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/base.rb:523:in `__send__'
        from /usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/base.rb:523:in `deliver!'
        from /usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/base.rb:395:in `method_missing'
        from test_sending_emails_in_ruby.rb:31 

this is the error message i got and it was solved when i added a recipient

Thursday, April 9, 2009

Gems vs Plugins

Which one to use GEMS or PLUGINS?
This was a question i always asked and i have searched on the internet for comparisons between each one and the other

i can see that people prefer gems for the sake that it is the new future, it has obligatory versions other than rails plugins

But as for me, i only see gems useful in these two cases
  1. when using code provided as gem outside of Rails, maybe with some ruby application
  2. if you want to share code written in gem among applications
other than that, i see plugins better and much more maintainable for each application on its own

Monday, March 9, 2009

Uploading Images from URL using Paperclip

I was searching for a way inside paperclip that uploads image from Url instead of File which is the normal behavior handled by Paperclip but I didn't find that

After too much search and trials, I reached to a solution that is easy to be implemented and handle all cases you can expect in this process

I am applying the solution inside my model but it is preferred to add a patch to Paperclip version to use so that the same logic be applied everywhere but this exercise is left for you to do

To check the code and how it evolved to handle all special cases, Please review my gist here

Tuesday, September 23, 2008

Rails Console Tips & Tricks II

This is my second rails console tips & tricks. if you interested in reading previous tips and tricks read my first blog post regarding that matter.

This second part is about one command called "load" that is available in your Rails console. you can use this command to do many things as

1. to reload any file that was loaded when starting console and don't get reloaded when using "reload!" command such as files in your lib folder. to reload any file just write

load "#{RAILS_ROOT}/lib/{{file_name}}"


2. if you faced a problem such as "MySQL has gone away" just reload environment.rb once again as follows

load "#{RAILS_ROOT}/config/environment.rb"


3. also if you want to test a bunch of code in your console not just a single line of code, and want to be able to change part in it and rerun it once again then doing so in console is very time consuming and annoying so rather than doing so, you can write this code in a file and then

load "{{your-ruby-file}}"

Monday, August 18, 2008

Different encoding error

if you found yourself suffering with this error

ActiveRecord::StatementInvalid: Mysql::Error: #HY000Incorrect string value

don't cry or check your code looking at the data you save thinking that you might have left some bad characters around causing you this misery

all you need to do is to check the used database connection encoding against your db tables encoding, most probably you will find them different

if so then the solution is one of these two options
1) remove encoding field from specification and use default db encoding
2) change the encoding of your db tables to this encoding

once you do so, you will have a good life

Tuesday, July 22, 2008

acts_as_modified plugin

is it changed? which attributes? what are the old values? in some cases you may be asking yourself these questions when dealing with some models

When should you care about these questions?

suppose you have a model doing an expensive operation on save. this operation is useful only if one specific attribute changed. so doing it on every save neglecting which attributes changed will degrades performance significantly

So what can you do?

you can load model old data from database and check if that attribute has changed or not and if so do your action

or

you can use acts_as_modified plugin, it helps keeping modified attributes with their old values within model in order to be capable of answering your questions without doing any extra database trips

I am exited, from where can i get it?

it depends on your rails version

1. ((rails 2.1)) do nothing, dirty objects already embedded in this version
2. ((rails r7315 and above)) install from http://svn.viney.net.nz/things/branches/acts_as_modified_edge
3. ((ELSE)) install from http://svn.viney.net.nz/things/rails/plugins/acts_as_modified

That's it, Enjoy the fun