public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
technoweenie (author)
Tue Mar 04 01:44:55 -0800 2008
commit  690efcf9364da291d62e9b83e192907c4e815fa4
tree    fa0038e15bd2dfe7a765138d79fd02510ab24d52
parent  2c876c3f03bcad10afd19f7a361ea18aadb773c2 parent  165d9a73cc9ec17f2f2b69c2155a54cd2596788d
mephisto / app / controllers / admin / assets_controller.rb
100644 150 lines (128 sloc) 4.908 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
class Admin::AssetsController < Admin::BaseController
  member_actions.push(*%w(index new create latest search add_bucket clear_bucket edit update))
  skip_before_filter :login_required
  before_filter :find_asset, :except => [:index, :new, :create, :latest, :search, :upload, :clear_bucket]
  before_filter :login_required
 
  def index
    search_assets 24
    @recent = []
    4.times { @recent << @assets.shift }
    @recent.compact!
    
    respond_to do |format|
      format.html
      format.js
    end
  end
  
  def new
    @asset = Asset.new
  end
 
  def create
    @assets = []
    params[:asset] ||= {} ; params[:asset_data] ||= []
    params[:asset].delete(:title) if params[:asset_data].size > 1
    params[:asset_data].each do |file|
      @assets << site.assets.build(params[:asset].merge(:uploaded_data => file, :user_id => current_user.id))
    end
    Asset.transaction { @assets.each &:save! }
    flash[:notice] = @assets.size == 1 ? "'#{CGI.escapeHTML @assets.first.title}' was uploaded." : "#{@assets.size} assets were uploaded."
    if @assets.size.zero?
      @asset = Asset.new
      render :action => 'new'
    else
      redirect_to assets_path
    end
  rescue ActiveRecord::RecordInvalid => e
    @asset = e.record
    render :action => 'new'
  end
 
  def update
    @asset.attributes = params[:asset]
    @asset.save!
    redirect_to assets_path
  rescue ActiveRecord::RecordInvalid
    render :action => 'edit'
  end
 
  def latest
    @assets = site.assets.find(:all, :order => 'created_at desc', :limit => 6)
    render :update do |page|
      page['latest-assets'].replace_html :partial => 'widget', :collection => @assets, :locals => { :prefix => 'latest' }
    end
  end
  
  def search
    search_assets 6
    render :update do |page|
      page['spinner'].hide
        return page['search-assets'].replace_html(:partial => 'widget', :collection => @assets, :locals => { :prefix => 'search' }) if @assets.any?
        page['search-assets'].replace_html %(Couldn't find any matching assets.)
    end
  end
 
  def destroy
    @asset.destroy
    redirect_to assets_path
    (session[:bucket] || {}).delete(@asset.public_filename)
    flash[:notice] = "Deleted '#{@asset.filename}'"
  end
 
  # rjs
  def add_bucket
    if (session[:bucket] ||= {}).key?(@asset.id)
      render :nothing => true and return
    end
    args = asset_image_args_for(@asset, :tiny, :title => "#{@asset.title} \n #{@asset.tags.join(', ')}")
    session[:bucket][@asset.id] = args
  end
 
  def clear_bucket
    session[:bucket] = nil
  end
 
  protected
    def find_asset
      @asset = site.assets.find(params[:id])
    end
 
    def search_assets(limit)
      @types = params[:filter].blank? ? [] : params[:filter].keys
      options = search_options.merge(:per_page => limit, :page => params[:page], :total_entries => count_by_conditions)
 
      @assets = @types.any? ?
        site.assets.paginate_by_content_types(@types, :all, options) :
        site.assets.paginate(options)
    end
 
    def search_options
      search_conditions.merge(:order => 'created_at desc')
    end
 
    def search_conditions
      return @search_conditions if @search_conditions
      unless params[:q].blank?
        params[:q].downcase!
        params[:q] << '%'
      end
      
      @search_conditions =
        returning :conditions => [] do |options|
          options[:include] = []
          unless params[:q].blank?
            params[:conditions] = { :title => true, :tags => true } if params[:conditions].blank?
            if params[:conditions].has_key?(:title)
              options[:conditions] << Asset.send(:sanitize_sql, ['(LOWER(assets.title) LIKE :q or LOWER(assets.filename) LIKE :q)', {:q => params[:q]}])
            end
            
            if params[:conditions].has_key?(:tags)
              options[:include] << :tags
              options[:conditions] << Asset.send(:sanitize_sql, ["(taggings.taggable_type = 'Asset' and tags.name IN (?))", Tag.parse(params[:q].chomp("%"))])
            end
          end
        
          if options[:conditions].blank?
            options.delete(:conditions)
          else
            options[:conditions] *= ' OR '
          end
          
          options.delete(:include) if options[:include].empty?
        end
    end
    
    def count_by_conditions
      type_conditions = @types.blank? ? nil : Asset.types_to_conditions(@types.dup).join(" OR ")
      @count_by_conditions ||= search_conditions[:conditions].blank? ? site.assets.count(:all, :conditions => type_conditions) :
        Asset.count(
        :joins => search_conditions[:joins],
        :conditions => "site_id = #{site.id} #{type_conditions && "and #{type_conditions}"} AND #{search_conditions[:conditions]}",
        :include => search_conditions[:include])
    end
    
    def allow_member?
      @asset && @asset.user_id.to_s == current_user.id.to_s
    end
end