Skip to content

Commit

Permalink
Merge branch 'dry_controllers' of github.com:fatfreecrm/fat_free_crm …
Browse files Browse the repository at this point in the history
…into dry_controllers
  • Loading branch information
ndbroadbent committed Dec 10, 2011
2 parents c2b608c + 4e616df commit 149cece
Show file tree
Hide file tree
Showing 18 changed files with 266 additions and 592 deletions.
68 changes: 9 additions & 59 deletions app/controllers/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,34 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#------------------------------------------------------------------------------

class AccountsController < ApplicationController
before_filter :require_user
class AccountsController < BaseController
before_filter :get_data_for_sidebar, :only => :index
before_filter :set_current_tab, :only => [ :index, :show ]
after_filter :update_recently_viewed, :only => :show

# GET /accounts
# GET /accounts.json
# GET /accounts.xml HTML and AJAX
#----------------------------------------------------------------------------
def index
@accounts = get_accounts(:page => params[:page])

respond_to do |format|
format.html # index.html.haml
format.js # index.js.rjs
format.json { render :json => @accounts }
format.xml { render :xml => @accounts }
format.xls { send_data @accounts.to_xls, :type => :xls }
format.csv { send_data @accounts.to_csv, :type => :csv }
format.rss { render "shared/index.rss.builder" }
format.atom { render "shared/index.atom.builder" }
end
respond_with(@accounts)
end

# GET /accounts/1
# GET /accounts/1.json
# GET /accounts/1.xml HTML
#----------------------------------------------------------------------------
def show
@account = Account.my.find(params[:id])

respond_to do |format|
respond_with(@account) do |format|
format.html do
@stage = Setting.unroll(:opportunity_stage)
@comment = Comment.new

@timeline = timeline(@account)
end
format.json { render :json => @account }
format.xml { render :xml => @account }
end

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

# GET /accounts/new
# GET /accounts/new.json
# GET /accounts/new.xml AJAX
#----------------------------------------------------------------------------
def new
@account = Account.new(:user => @current_user, :access => Setting.default_access)
Expand All @@ -74,11 +52,7 @@ def new
instance_variable_set("@#{model}", model.classify.constantize.find(id))
end

respond_to do |format|
format.js # new.js.rjs
format.json { render :json => @account }
format.xml { render :xml => @account }
end
respond_with(@account)
end

# GET /accounts/1/edit AJAX
Expand All @@ -89,55 +63,39 @@ def edit
if params[:previous].to_s =~ /(\d+)\z/
@previous = Account.my.find($1)
end
respond_with(@account)

rescue ActiveRecord::RecordNotFound
@previous ||= $1.to_i
respond_to_not_found(:js) unless @account
end

# POST /accounts
# POST /accounts.json
# POST /accounts.xml AJAX
#----------------------------------------------------------------------------
def create
@account = Account.new(params[:account])
@users = User.except(@current_user)

respond_to do |format|
respond_with(@account) do |format|
if @account.save_with_permissions(params[:users])
# None: account can only be created from the Accounts index page, so we
# don't have to check whether we're on the index page.
@accounts = get_accounts
get_data_for_sidebar
format.js # create.js.rjs
format.json { render :json => @account, :status => :created, :location => @account }
format.xml { render :xml => @account, :status => :created, :location => @account }
else
format.js # create.js.rjs
format.json { render :json => @account.errors, :status => :unprocessable_entity }
format.xml { render :xml => @account.errors, :status => :unprocessable_entity }
end
end
end

# PUT /accounts/1
# PUT /accounts/1.json
# PUT /accounts/1.xml AJAX
#----------------------------------------------------------------------------
def update
@account = Account.my.find(params[:id])

respond_to do |format|
respond_with(@account) do |format|
if @account.update_with_permissions(params[:account], params[:users])
get_data_for_sidebar
format.js
format.json { head :ok }
format.xml { head :ok }
else
@users = User.except(@current_user) # Need it to redraw [Edit Account] form.
format.js
format.json { render :json => @account.errors, :status => :unprocessable_entity }
format.xml { render :xml => @account.errors, :status => :unprocessable_entity }
end
end

Expand All @@ -146,17 +104,14 @@ def update
end

# DELETE /accounts/1
# DELETE /accounts/1.xml HTML and AJAX
#----------------------------------------------------------------------------
def destroy
@account = Account.my.find(params[:id])
@account.destroy if @account

respond_to do |format|
respond_with(@account) do |format|
format.html { respond_to_destroy(:html) }
format.js { respond_to_destroy(:ajax) }
format.json { head :ok }
format.xml { head :ok }
end

rescue ActiveRecord::RecordNotFound
Expand All @@ -168,20 +123,16 @@ def destroy
def search
@accounts = get_accounts(:query => params[:query], :page => 1)

respond_to do |format|
respond_with(@accounts) do |format|
format.js { render :index }
format.json { render :json => @accounts }
format.xml { render :xml => @accounts }
end
end

# PUT /accounts/1/attach
# PUT /accounts/1/attach.xml AJAX
#----------------------------------------------------------------------------
# Handled by ApplicationController :attach

# PUT /accounts/1/discard
# PUT /accounts/1/discard.xml AJAX
#----------------------------------------------------------------------------
# Handled by ApplicationController :discard

Expand Down Expand Up @@ -264,4 +215,3 @@ def get_data_for_sidebar
@account_category_total[:other] = @account_category_total[:all] - categorized
end
end

113 changes: 0 additions & 113 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,83 +31,6 @@ class ApplicationController < ActionController::Base
# Uncomment the :secret if you're not using the cookie session store
# protect_from_forgery # :secret => '165eb65bfdacf95923dad9aea10cc64a'

# Common auto_complete handler for all core controllers.
#----------------------------------------------------------------------------
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

# Common attach handler for all core controllers.
#----------------------------------------------------------------------------
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

# Common discard handler for all core controllers.
#----------------------------------------------------------------------------
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

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

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

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
private
#----------------------------------------------------------------------------
def set_context
Expand Down Expand Up @@ -249,42 +172,6 @@ def respond_to_related_not_found(related, *types)
end
end

# Get list of records for a given model class.
#----------------------------------------------------------------------------
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

# Proxy current page for any of the controllers by storing it in a session.
#----------------------------------------------------------------------------
def current_page=(page)
Expand Down

0 comments on commit 149cece

Please sign in to comment.