Save an image from url to a file using Dragonfly by dskecse

C38db09277e3ce12966c1a030265a1c5?size=52

Given an image accessor field cover_image for an Image model, u can use a cover_image_url accessor for assigning directly from a url (simply using a string). To make it does what u want just save the corresponding object.

example:

img = Image.new
img.cover_image_url = 'http://img.youtube.com/vi/wh-XcmRQFhI/0.jpg'
img.save

BTW: what is your favourite image uploader in rails?

#dragonfly #rails

Custom helper methods of State Machine (ruby) by releu

757fb0d5ec7560b6f25f5bd98eadc020?size=52

I have a lot of code in my project like the following:

class Animal < ActiveRecord::Base
  state_machine do
    around_transition :on => :sure do |animal, _, block|
      animal.transaction do
        block.call
        animal.do_things_on_sure
      end
    end
  end
end

around_transition looks ugly and repeats for many classes.. Lets refactor it. More under the cut

Using external resources in callbacks in rails by releu

757fb0d5ec7560b6f25f5bd98eadc020?size=52

Here is a typical bad case in #rails:

class Band < ActiveRecord::Base
  has_many :members, dependent: :destroy
end

class Member < ActiveRecord::Base
  belongs_to :band

  after_destroy do
    Resque.enqueue BadNewsWorker, id
    raise('surprise') # real world :(
  end
end

band = Band.create
band.members.create
band.destroy # => RuntimeError
Band.count => 1

More under the cut

Canceling validations in ActiveRecord by Mik-die

7d116b912a4fc7986b40d5d0d0d811d6?size=52

Sometimes there are some external validations that are brought in our #active_record models by some gems. Usually these validations are useful, but sometimes we don't want such validations, if we want redefine some gem's behavior.

So, I show you how to cancel validation in ActiveRecord model, for example Devise's validation of email uniqueness. More under the cut

Using capture in templates by releu

757fb0d5ec7560b6f25f5bd98eadc020?size=52

You can refactor template's code with capture method:

/ before:
li
  = mail_to 'info@gistflow.com', '<i class="icon-envelope"></i><span>Email</span>'.html_safe, class: 'secondary', encode: 'hex'

/ after:
li
  - name = capture do
    i class="icon-envelope"
    span Email
  = mail_to 'info@gistflow.com', name, class: 'secondary', encode: 'hex'

Very useful :)

Ruby on Rails API

#rails

content_tag and helpers (rails) by sebkomianos

B130aa7ae93d8ea344146795da430cef?size=52

I am trying to create a calendar table to display the daily availability of every staff member of a company. What I think I need is a header with the days of the month and then a row (with as many cells as the number of the days) for each staff member. The different background color of each cell will represent a different availability status and the staff members change so I want all this to be dynamically generated. Click for More!