Skip to content

Commit

Permalink
refactor with names_scope the search article. I think is more lisible…
Browse files Browse the repository at this point in the history
… and more use about SKINNY controller, FAT Model
  • Loading branch information
shingara committed Dec 3, 2008
1 parent fee2e74 commit 6f8ecf6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 42 deletions.
47 changes: 5 additions & 42 deletions app/controllers/admin/content_controller.rb
Expand Up @@ -10,54 +10,17 @@ def auto_complete_for_article_keywords
render :inline => "<%= auto_complete_result @items, 'name' %>"
end

def build_filter_params
@conditions = ["state <> 'draft'"]
if params[:search]
@search = params[:search]

if @search[:searchstring]
tokens = @search[:searchstring].split.collect {|c| "%#{c.downcase}%"}
@conditions = [(["(LOWER(body) LIKE ? OR LOWER(extended) LIKE ? OR LOWER(title) LIKE ?)"] * tokens.size).join(" AND "), *tokens.collect { |token| [token] * 3 }.flatten]
end

if @search[:published_at] and %r{(\d\d\d\d)-(\d\d)} =~ @search[:published_at]
@conditions[0] += " AND published_at LIKE ? "
@conditions << "%#{@search[:published_at]}%"
end

if @search[:user_id] and @search[:user_id].to_i > 0
@conditions[0] += " AND user_id = ? "
@conditions << @search[:user_id]
end

if @search[:published] and @search[:published].to_s =~ /0|1/
@conditions[0] += " AND published = ? "
@conditions << @search[:published]
end

if @search[:category] and @search[:category].to_i > 0
@conditions[0] += " AND categorizations.category_id = ? "
@conditions << @search[:category]
end

else
@search = { :category => nil, :user_id => nil, :published_at => nil, :published => nil }
end
end

def index
@drafts = Article.find(:all, :conditions => "state='draft'")
now = Time.now
build_filter_params
@drafts = Article.draft.all
setup_categories
@articles = Article.paginate :page => params[:page], :conditions => @conditions, :order => 'created_at DESC', :per_page => 10
@search = params[:search] ? params[:search] : {}
@articles = Article.search_no_draft_paginate(@search, :page => params[:page], :per_page => 10)

if request.xhr?
render :partial => 'article_list', :object => @articles
return
else
@article = Article.new(params[:article])
end

@article = Article.new(params[:article])
end

def show
Expand Down
45 changes: 45 additions & 0 deletions app/models/article.rb
Expand Up @@ -46,6 +46,51 @@ def spam

has_and_belongs_to_many :tags, :foreign_key => 'article_id'

named_scope :published_at_like, lambda {|date_at| {:conditions => ['published_at LIKE ? ', "%#{date_at}%"]}}
named_scope :user_id, lambda {|user_id| {:conditions => ['user_id = ?', user_id]}}
named_scope :published, {:conditions => ['published = ?', true]}
named_scope :not_published, {:conditions => ['published = ?', false]}
named_scope :category, lambda {|category_id| {:conditions => ['categorizations.category_id = ?', category_id], :include => 'categorizations'}}
named_scope :draft, {:conditions => ['state = ?', 'draft']}
named_scope :no_draft, {:conditions => ['state <> ?', 'draft'], :order => 'created_at DESC'}
named_scope :searchstring, lambda {|search_string|
tokens = search_string.split.collect {|c| "%#{c.downcase}%"}
{:conditions => [(['(LOWER(body) LIKE ? OR LOWER(extended) LIKE ? OR LOWER(title) LIKE ?)']*tokens.size).join(' AND '),
*tokens.collect{ |token| [token] * 3 }.flatten]}
}

def self.search_no_draft_paginate(search_hash, paginate_hash)
list_function = ["Article.no_draft"]
if search_hash.nil?
search_hash = {}
end

if search_hash[:searchstring]
list_function << 'searchstring(search_hash[:searchstring])'
end

if search_hash[:published_at] and %r{(\d\d\d\d)-(\d\d)} =~ search_hash[:published_at]
list_function << 'published_at_like(search_hash[:published_at])'
end

if search_hash[:user_id] && search_hash[:user_id].to_i > 0
list_function << 'user_id(user_id)'
end

if search_hash[:published]
list_function << 'published' if search_hash[:published].to_s == '1'
list_function << 'not_published' if search_hash[:published].to_s == '0'
end

if search_hash[:category] and search_hash[:category].to_i > 0
list_function << 'category(search_hash[:category])'
end

paginate_hash[:order] = 'created_at DESC'
list_function << "paginate(paginate_hash)"
eval(list_function.join('.'))
end

belongs_to :user

has_many :triggers, :as => :pending_item
Expand Down

0 comments on commit 6f8ecf6

Please sign in to comment.