25e782eb7e799e78d781b0026fc6a4d8

this is a little piece of code that can display different stuff based on where we are coming from, it's a shallow route. better ways?

# controller

class ArticoliController < ApplicationController

  before_filter :find_scope, :only => [ :index, :new, :create ]

  def index
    if params[:bolla_da_fornitore_id]
      @articoli = @carico.articoli.search(:term => params[:q], :page => params[:page])
      render 'carichi/articoli'
    elsif params[:bolla_verso_redazione_id]
      @articoli = @uscita.articoli.search(:term => params[:q], :page => params[:page])
      render 'uscite/articoli'
    elsif params[:bolla_verso_fornitore_id]
      @articoli = @reso.articoli.search(:term => params[:q], :page => params[:page])
      render 'resi/articoli'
    elsif params[:bolla_da_redazione_id]
      @articoli = @rientro.articoli.search(:term => params[:q], :page => params[:page])
      render 'rientri/articoli'
    else
      @articoli_grid = initialize_grid(Articolo, :include => :fornitore, :joins => [:linea, :stagione], :name => 'articoli_grid')
    end
  end

  private
  def find_scope
    @carico  = BollaDaFornitore.find(params[:bolla_da_fornitore_id]) if params[:bolla_da_fornitore_id]
    @rientro = BollaDaRedazione.find(params[:bolla_da_redazione_id]) if params[:bolla_da_redazione_id]
    @uscita  = BollaVersoRedazione.find(params[:bolla_verso_redazione_id]) if params[:bolla_verso_redazione_id]
    @reso    = BollaVersoFornitore.find(params[:bolla_verso_fornitore_id]) if params[:bolla_verso_fornitore_id]
  end

# module
module Searchable

  def self.included(base)
    base.class_eval do
      extend ClassMethods
      cattr_accessor :searchable_attrs
    end
  end

  module ClassMethods

    def search(*args)
      options = args.extract_options!
      options[:per_page] ||= 10
      options[:page] ||= 1
      options[:order] ||= 'id ASC'
      term = options.delete(:term).to_s
      if term.present?
        options.merge!({:conditions => [self.searchable_attrs.collect { |c| c + " LIKE :term" }.join(" OR ") , { :term => '%' + term + '%' }]})
      end
      paginate(options)
    end
  end

end

# model
class Articolo < ActiveRecord::Base
  include Searchable
  self.searchable_attrs = %w(codice descrizione ean_13).freeze

Refactorings

No refactoring yet !

A8d3f35baafdaea851914b17dae9e1fc

Adam

October 17, 2009, October 17, 2009 13:20, permalink

No rating. Login to rate!
class ArticoliController < ApplicationController
  AVAILABLE_SCOPES = [ BollaDaFornitore, BollaDaRedazione, BollaVersoRedazione, BollaVersoFornitore ]
  
  def index
    @articoli = scope.articoli.search(:term => params[:q], :page => params[:page])
  end
  
  private
    def scope
      AVAILABLE_SCOPES.inject(nil) do |klass|
        break klass if params["#{klass.to_s.underscore}_id"]
      end || Articoli
    end
end
83cef5e0ca6b31e30b85bfa5f04d8cb0

Colin Curtin

October 19, 2009, October 19, 2009 19:19, permalink

No rating. Login to rate!

A refactoring of Adam's refactoring

def scope
  AVAILABLE_SCOPES.find{|klass| params["#{klass.to_s.underscore}_id"]} || Articoli
end
A8d3f35baafdaea851914b17dae9e1fc

Adam

October 22, 2009, October 22, 2009 02:56, permalink

No rating. Login to rate!

Good call on find. That's what I get for not writing much Ruby code these days.

Your refactoring





Format Copy from initial code

or Cancel