Class: BaseController

Inherits:
ApplicationController show all
Defined in:
app/controllers/base_controller.rb

Overview

Fat Free CRM Copyright © 2008-2011 by Michael Dvorkin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.


Instance Method Summary collapse

Methods inherited from ApplicationController

#klass

Instance Method Details

#attachObject

Common attach handler for all core controllers.




49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/base_controller.rb', line 49

def attach
  model = klass.my.find(params[:id])
  @attachment = params[:assets].classify.constantize.find(params[:asset_id])
  @attached = model.attach!(@attachment)
  @account  = model.reload if model.is_a?(Account)
  @campaign = model.reload if model.is_a?(Campaign)

  respond_to do |format|
    format.js   { render "shared/attach" }
    format.json { render :json => model.reload }
    format.xml  { render :xml => model.reload }
  end

rescue ActiveRecord::RecordNotFound
  respond_to_not_found(:html, :js, :json, :xml)
end

#auto_completeObject

Common auto_complete handler for all core controllers.




32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/base_controller.rb', line 32

def auto_complete
  @query = params[:auto_complete_query]
  @auto_complete = hook(:auto_complete, self, :query => @query, :user => @current_user)
  if @auto_complete.empty?
    @auto_complete = klass.my.text_search(@query).limit(10)
  else
    @auto_complete = @auto_complete.last
  end
  session[:auto_complete] = controller_name.to_sym
  respond_to do |format|
    format.any(:js, :html)   { render "shared/auto_complete", :layout => nil }
    format.json { render :json => @auto_complete.inject({}){|h,a| h[a.id] = a.name; h } }
  end
end

#discardObject

Common discard handler for all core controllers.




68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/base_controller.rb', line 68

def discard
  model = klass.my.find(params[:id])
  @attachment = params[:attachment].constantize.find(params[:attachment_id])
  model.discard!(@attachment)
  @account  = model.reload if model.is_a?(Account)
  @campaign = model.reload if model.is_a?(Campaign)

  respond_to do |format|
    format.js   { render "shared/discard" }
    format.json { render :json => model.reload }
    format.xml  { render :xml => model.reload }
  end

rescue ActiveRecord::RecordNotFound
  respond_to_not_found(:html, :js, :json, :xml)
end

#field_groupObject



98
99
100
101
102
103
104
105
106
# File 'app/controllers/base_controller.rb', line 98

def field_group
  if @tag = Tag.find_by_name(params[:tag].strip)
    if @field_group = FieldGroup.find_by_tag_id_and_klass_name(@tag.id, klass.to_s)
      @asset = klass.find_by_id(params[:asset_id]) || klass.new
      render 'fields/group' and return
    end
  end
  render :text => ''
end

#taggedObject

Controller instance method that responds to /controlled/tagged/tag request. It stores given tag as current query and redirect to index to display all records tagged with the tag.




93
94
95
96
# File 'app/controllers/base_controller.rb', line 93

def tagged
  self.send(:current_query=, "#" << params[:id]) unless params[:id].blank?
  redirect_to :action => "index"
end

#timeline(asset) ⇒ Object



85
86
87
# File 'app/controllers/base_controller.rb', line 85

def timeline(asset)
  (asset.comments + asset.emails).sort { |x, y| y.created_at <=> x.created_at }
end