Monthly Archives: April 2014

Removing Paperclip attachments

Paperclip’s GitHub page includes the necessary steps to delete an attachment – the model’s attribute that refers to the attachment simply gets set to nil. Here is the full implementation of deleting a user’s profile picture in a Rails app. Start with tests (right?!):

spec/features/editing_users_spec.rb:

require 'spec_helper'

feature 'Editing Users' do
  let(:user) { FactoryGirl.create(:user) }
  let(:admin) { FactoryGirl.create(:admin) }

  scenario 'removing user profile photo' do
    sign_in_as admin # of course
    visit edit_user_path(user)
    click_link 'Remove profile photo'

    expect(page).to have_content 'User profile photo has been removed.'
    expect(page).not_to have_css("img[src*='user.png']") # file name of the user photo defined in the factory
    expect(page).to have_css("img[src*='paperclip_default.png']") # specified in the User model
  end
end

Continue reading

Advertisements
Tagged , ,

Paperclip attachments in RSpec factories

Assuming one has a model whose attachments are handled by Paperclip, i.e.

$ rails generate paperclip user photo

app/models/user.rb:

class User < ActiveRecord::Base

  has_attached_file :photo
end

then to generate a test model with FactoryGirl, including the attachment: Continue reading

Tagged , , ,

Testing images with RSpec

To test whether the right image is displayed on a page:


expect(page).to have_css("img[src*='image_name.png']")

i.e. “Expect page to have an <img> tag whose src attribute contains the name of the required image.”

Tagged , ,

Learn to delegate (your model methods)

delegate is neat trick that allows one to write cleaner, neater code. Particularly when dealing with ActiveRecord associations, delegate makes it possible to “outsource” a method to an associated object.

class Book < ActiveRecord::Base

  # has attribute 'author'

end

class Chapter < ActiveRecord::Base

  belongs_to :book

  delegate :author, to: :book

end

Continue reading

Tagged , ,

Is it changed?

Rails has some useful methods to check whether an object has changed since first being loaded into memory. Here are some examples:


$ rails console --sandbox

2.1.0 :001 > user = User.find(1)

 => #<User id: 1, name: "Roderick", email: "rod@example.com", created_at: ..., updated_at: ...>

2.1.0 :002 > user.name = 'Jimmy'

 => "Jimmy"

2.1.0 :003 > user.changed?

 => true

2.1.0 :004 > user.changed

 => ["name"]

2.1.0 :005 > user.changes

 => {"name"=>["Roderick", "Jimmy"]}

2.1.0 :006 > user.name_changed?

 => true

2.1.0 :007 > user.email_changed?

 => false

2.1.0 :008 > user.name_was

 => "Roderick"

2.1.0 :009 > user.save

 => true

2.1.0 :010 > user.changed?

 => false

More info here: ActiveModel::Dirty.

Tagged , ,

cycle View Helper

The cycle helper method alternates between the values given to it each time it is called:

$ rails console

2.1.0 :001 > include ActionView::Helpers

2.1.0 :002 > cycle('red', 'white')
 => "red"

2.1.0 :003 > cycle('red', 'white')
 => "white"

Continue reading

Tagged , ,

Defining CanCan Abilities with blocks

This brainteaser came courtesy of Rails 4 in Action and can be found therein as Listing 8.7. The intended purpose of the code is to use the CanCan gem to define user abilities, or more specifically “can”s (such as “can create a project” or “can edit a ticket”) based on permissions given to him by an admin:

app/models/ability.rb:

class Ability
  include CanCan::Ability

  def initialize(user)
    user.permissions.each do |permission|
      can permission.action.to_sym,
          permission.thing_type.constantize do |thing|
            thing.nil? ||
            permission.thing_id.nil? ||
            permission.thing_id == thing.id
      end
    end
  end
end

Continue reading

Tagged ,

do..end vs {}: Not just a matter of style!

“Use curly braces for single-line blocks and do..end for multi-lines blocks.” Having seen various permutations of that statement dozens of times, I came to believe that the difference between the two was purely stylistic and that there was no practical reason to opt for one vs. the other. Most of the time, that is, indeed, true. Until that one-time-in-a-thousand when it’s not. Continue reading

Tagged , , , ,