Fat Free CRM Copyright (C) 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/>.

Methods
A
C
D
F
G
P
R
S
T
U
Instance Public methods
attach()

Common attach handler for all core controllers.

# File app/controllers/application_controller.rb, line 50
def attach
  model = controller_name.classify.constantize.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_complete()

Common #auto_complete handler for all core controllers.

# File app/controllers/application_controller.rb, line 36
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 = controller_name.classify.constantize.my.search(@query).limit(10)
  else
    @auto_complete = @auto_complete.last
  end
  session[:auto_complete] = controller_name.to_sym
  render "shared/auto_complete", :layout => nil
end
discard()

Common discard handler for all core controllers.

# File app/controllers/application_controller.rb, line 69
def discard
  model = controller_name.classify.constantize.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_group()
# File app/controllers/application_controller.rb, line 99
def field_group
  if @tag = ActsAsTaggableOn::Tag.find_by_name(params[:tag].strip) and
     @field_group = FieldGroup.find_by_tag_id(@tag.id)

    klass = controller_name.classify.constantize
    @asset = klass.find_by_id(params[:asset_id]) || klass.new

    render 'fields/group'
  else
    render :text => ''
  end
end
tagged()

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.

# File app/controllers/application_controller.rb, line 94
def tagged
  self.send(:current_query=, "#" << params[:id]) unless params[:id].blank?
  redirect_to :action => "index"
end
timeline(asset)
# File app/controllers/application_controller.rb, line 86
def timeline(asset)
  (asset.comments + asset.emails).sort { |x, y| y.created_at <=> x.created_at }
end
Instance Private methods
called_from_index_page?(controller = controller_name)
# File app/controllers/application_controller.rb, line 191
def called_from_index_page?(controller = controller_name)
  if controller != "tasks"
    request.referer =~ %r(/#{controller}$)
  else
    request.referer =~ /tasks\?*/
  end
end
called_from_landing_page?(controller = controller_name)
# File app/controllers/application_controller.rb, line 200
def called_from_landing_page?(controller = controller_name)
  request.referer =~ %r(/#{controller}/\w+)
end
can_signup?()
# File app/controllers/application_controller.rb, line 186
def can_signup?
  [ :allowed, :needs_approval ].include? Setting.user_signup
end
current_page()
# File app/controllers/application_controller.rb, line 295
def current_page
  page = params[:page] || session["#{controller_name}_current_page".to_sym] || 1
  @current_page = page.to_i
end
current_page=(page)

Proxy current page for any of the controllers by storing it in a session.

# File app/controllers/application_controller.rb, line 290
def current_page=(page)
  @current_page = session["#{controller_name}_current_page".to_sym] = page.to_i
end
current_query()
# File app/controllers/application_controller.rb, line 307
def current_query
  @current_query = params[:query] || session["#{controller_name}_current_query".to_sym] || ""
end
current_query=(query)

Proxy current search query for any of the controllers by storing it in a session.

# File app/controllers/application_controller.rb, line 302
def current_query=(query)
  @current_query = session["#{controller_name}_current_query".to_sym] = query
end
current_user()
# File app/controllers/application_controller.rb, line 144
def current_user
  @current_user ||= (current_user_session && current_user_session.record)
  if @current_user
    @current_user.set_individual_locale
    @current_user.set_single_access_token
  end
  User.current_user = @current_user
end
current_user_session()
# File app/controllers/application_controller.rb, line 135
def current_user_session
  @current_user_session ||= Authentication.find
  if @current_user_session && @current_user_session.record.suspended?
    @current_user_session = nil
  end
  @current_user_session
end
get_list_of_records(klass, options = {})

Get list of records for a given model class.

# File app/controllers/application_controller.rb, line 254
def get_list_of_records(klass, options = {})
  items = klass.name.tableize
  self.current_page = options[:page]                        if options[:page]
  query, tags       = parse_query_and_tags(options[:query]) if options[:query]
  self.current_query = query

  records = {
    :user  => @current_user,
    :order => @current_user.pref[:"#{items}_sort_by"] || klass.sort_by
  }
  pages = {
    :page     => current_page,
    :per_page => @current_user.pref[:"#{items}_per_page"]
  }

  # Call the hook and return its output if any.
  assets = hook(:"get_#{items}", self, :records => records, :pages => pages)
  return assets.last unless assets.empty?

  # Use default processing if no hooks are present. Note that comma-delimited
  # export includes deleted records, and the pagination is enabled only for
  # plain HTTP, Ajax and XML API requests.
  wants = request.format
  filter = session[options[:filter]].to_s.split(',') if options[:filter]

  scope = klass.my(records)
  scope = scope.state(filter)                   if filter.present?
  scope = scope.search(query)                   if query.present?
  scope = scope.tagged_with(tags, :on => :tags) if tags.present?
  scope = scope.unscoped                        if wants.csv?
  scope = scope.paginate(pages)                 if wants.html? || wants.js? || wants.xml?
  scope
end
parse_query_and_tags(search_string)

Somewhat simplistic parser that extracts query and hash-prefixed tags from the search string and returns them as two element array, for example:

“real Billy Bones pirate” => [ “Billy Bones”, “real, pirate” ]

# File app/controllers/application_controller.rb, line 316
def parse_query_and_tags(search_string)
  query, tags = [], []
  search_string.scan(/[\w@\-\.#]+/).each do |token|
    if token.starts_with?("#")
      tags << token[1 .. -1]
    else
      query << token
    end
  end
  [ query.join(" "), tags.join(", ") ]
end
redirect_back_or_default(default)
# File app/controllers/application_controller.rb, line 180
def redirect_back_or_default(default)
  redirect_to(session[:return_to] || default)
  session[:return_to] = nil
end
require_no_user()
# File app/controllers/application_controller.rb, line 166
def require_no_user
  if current_user
    store_location
    flash[:notice] = t(:msg_logout_needed)
    redirect_to profile_url
  end
end
require_user()
# File app/controllers/application_controller.rb, line 154
def require_user
  unless current_user
    store_location
    flash[:notice] = t(:msg_login_needed) if request.fullpath != "/"
    respond_to do |format|
      format.html { redirect_to login_url }
      format.js   { render(:index) { |page| page.redirect_to login_url } }
    end
  end
end
respond_to_not_found(*types)
# File app/controllers/application_controller.rb, line 213
def respond_to_not_found(*types)
  asset = self.controller_name.singularize
  flick = case self.action_name
    when "destroy" then "delete"
    when "promote" then "convert"
    else self.action_name
  end
  if self.action_name == "show"
    # If asset does exist, but is not viewable to the current user..
    if asset.capitalize.constantize.exists?(params[:id])
      flash[:warning] = t(:msg_asset_not_authorized, asset)
    else
      flash[:warning] = t(:msg_asset_not_available, asset)
    end
  else
    flash[:warning] = t(:msg_cant_do, :action => flick, :asset => asset)
  end
  respond_to do |format|
    format.html { redirect_to :action => :index }                          if types.include?(:html)
    format.js   { render(:update) { |page| page.reload } }                 if types.include?(:js)
    format.json { render :text => flash[:warning], :status => :not_found } if types.include?(:json)
    format.xml  { render :text => flash[:warning], :status => :not_found } if types.include?(:xml)
  end
end
set_context()
# File app/controllers/application_controller.rb, line 113
  def set_context
    Time.zone = ActiveSupport::TimeZone[session[:timezone_offset]] if session[:timezone_offset]
    I18n.locale = Setting.locale if Setting.locale

    # Check if the latest settings have been loaded. Display error message in English
    # the actual locale might be unknown.
    if !Setting.locale || !Setting.account_category
      raise FatFreeCRM::ObsoleteSettings, "        It looks like you are upgrading from the older version of Fat Free CRM.
        Please review <b>config/settings.yml</b> file and re-run<br><br><b>$ rake
        crm:settings:load</b><br><br> command in Rails development and production
        environments.
"
    end
  end
set_current_tab(tab = controller_name)
# File app/controllers/application_controller.rb, line 130
def set_current_tab(tab = controller_name)
  @current_tab = tab
end
store_location()
# File app/controllers/application_controller.rb, line 175
def store_location
  session[:return_to] = request.fullpath
end
update_recently_viewed()
# File app/controllers/application_controller.rb, line 205
def update_recently_viewed
  subject = instance_variable_get("@#{controller_name.singularize}")
  if subject
    Activity.log(@current_user, subject, :viewed)
  end
end