Merge pull request #570 from AyuntamientoMadrid/refactoring_controllers

Refactoring controllers
This commit is contained in:
Juanjo Bazán
2015-09-29 16:02:32 +02:00
4 changed files with 140 additions and 147 deletions

View File

@@ -0,0 +1,106 @@
module CommentableActions
extend ActiveSupport::Concern
def index
@resources = @search_terms.present? ? resource_model.search(@search_terms) : resource_model.all
@resources = @resources.tagged_with(@tag_filter) if @tag_filter
@resources = @resources.page(params[:page]).for_render.send("sort_by_#{@current_order}")
@tag_cloud = tag_cloud
set_resource_votes(@resources)
set_resources_instance
end
def show
set_resource_votes(resource)
@commentable = resource
@root_comments = resource.comments.roots.recent.page(params[:page]).per(10).for_render
@comments = @root_comments.inject([]){|all, root| all + Comment.descendants_of(root).for_render}
@all_visible_comments = @root_comments + @comments
set_comment_flags(@all_visible_comments)
set_resource_instance
end
def new
@resource = resource_model.new
set_resource_instance
load_featured_tags
end
def create
@resource = resource_model.new(strong_params)
@resource.author = current_user
if @resource.save_with_captcha
track_event
redirect_to @resource, notice: t('flash.actions.create.notice', resource_name: "#{resource_name.capitalize}")
else
load_featured_tags
set_resource_instance
render :new
end
end
def edit
load_featured_tags
end
def update
resource.assign_attributes(strong_params)
if resource.save_with_captcha
redirect_to resource, notice: t('flash.actions.update.notice', resource_name: "#{resource_name.capitalize}")
else
load_featured_tags
set_resource_instance
render :edit
end
end
private
def resource
@resource ||= instance_variable_get("@#{resource_name}")
end
def resource_name
@resource_name ||= resource_model.to_s.downcase
end
def set_resource_instance
instance_variable_set("@#{resource_name}", @resource)
end
def set_resources_instance
instance_variable_set("@#{resource_name.pluralize}", @resources)
end
def set_resource_votes(instance)
send("set_#{resource_name}_votes", instance)
end
def strong_params
send("#{resource_name}_params")
end
def track_event
ahoy.track "#{resource_name}_created".to_sym, "#{resource_name}_id": resource.id
end
def tag_cloud
resource_model.tag_counts.order("#{resource_name.pluralize}_count": :desc, name: :asc).limit(20)
end
def load_featured_tags
@featured_tags = ActsAsTaggableOn::Tag.where(featured: true)
end
def parse_tag_filter
if params[:tag].present?
@tag_filter = params[:tag] if ActsAsTaggableOn::Tag.named(params[:tag]).exists?
end
end
def parse_search_terms
@search_terms = params[:search] if params[:search].present?
end
end

View File

@@ -0,0 +1,20 @@
module FlagActions
extend ActiveSupport::Concern
def flag
Flag.flag(current_user, flaggable)
respond_with flaggable, template: "#{controller_name}/_refresh_flag_actions"
end
def unflag
Flag.unflag(current_user, flaggable)
respond_with flaggable, template: "#{controller_name}/_refresh_flag_actions"
end
private
def flaggable
instance_variable_get("@#{resource_model.to_s.downcase}")
end
end

View File

@@ -1,96 +1,29 @@
class DebatesController < ApplicationController
before_action :parse_tag_filter, only: :index
include CommentableActions
include FlagActions
before_action :parse_search_terms, only: :index
before_action :parse_tag_filter, only: :index
before_action :authenticate_user!, except: [:index, :show]
has_orders %w{hot_score confidence_score created_at most_commented random}, only: :index
load_and_authorize_resource
respond_to :html, :js
def index
@debates = @search_terms.present? ? Debate.search(@search_terms) : Debate.all
@debates = @debates.tagged_with(@tag_filter) if @tag_filter
@debates = @debates.page(params[:page]).for_render.send("sort_by_#{@current_order}")
@tag_cloud = Debate.tag_counts.order(debates_count: :desc, name: :asc).limit(20)
set_debate_votes(@debates)
end
def show
set_debate_votes(@debate)
@commentable = @debate
@root_comments = @debate.comments.roots.recent.page(params[:page]).per(10).for_render
@comments = @root_comments.inject([]){|all, root| all + Comment.descendants_of(root).for_render}
@all_visible_comments = @root_comments + @comments
set_comment_flags(@all_visible_comments)
end
def new
@debate = Debate.new
load_featured_tags
end
def edit
load_featured_tags
end
def create
@debate = Debate.new(debate_params)
@debate.author = current_user
if @debate.save_with_captcha
ahoy.track :debate_created, debate_id: @debate.id
redirect_to @debate, notice: t('flash.actions.create.notice', resource_name: 'Debate')
else
load_featured_tags
render :new
end
end
def update
@debate.assign_attributes(debate_params)
if @debate.save_with_captcha
redirect_to @debate, notice: t('flash.actions.update.notice', resource_name: 'Debate')
else
load_featured_tags
render :edit
end
end
def vote
@debate.register_vote(current_user, params[:value])
set_debate_votes(@debate)
end
def flag
Flag.flag(current_user, @debate)
respond_with @debate, template: 'debates/_refresh_flag_actions'
end
def unflag
Flag.unflag(current_user, @debate)
respond_with @debate, template: 'debates/_refresh_flag_actions'
end
private
def debate_params
params.require(:debate).permit(:title, :description, :tag_list, :terms_of_service, :captcha, :captcha_key)
end
def load_featured_tags
@featured_tags = ActsAsTaggableOn::Tag.where(featured: true)
end
def parse_tag_filter
if params[:tag].present?
@tag_filter = params[:tag] if ActsAsTaggableOn::Tag.named(params[:tag]).exists?
end
end
def parse_search_terms
@search_terms = params[:search] if params[:search].present?
def resource_model
Debate
end
end

View File

@@ -1,72 +1,16 @@
class ProposalsController < ApplicationController
before_action :parse_tag_filter, only: :index
include CommentableActions
include FlagActions
before_action :parse_search_terms, only: :index
before_action :parse_tag_filter, only: :index
before_action :authenticate_user!, except: [:index, :show]
has_orders %w{hot_score confidence_score created_at most_commented random}, only: :index
load_and_authorize_resource
respond_to :html, :js
def index
@proposals = @search_terms.present? ? Proposal.search(@search_terms) : Proposal.all
@proposals = @proposals.tagged_with(@tag_filter) if @tag_filter
@proposals = @proposals.page(params[:page]).for_render.send("sort_by_#{@current_order}")
@tag_cloud = Proposal.tag_counts.order(proposals_count: :desc, name: :asc).limit(20)
set_proposal_votes(@proposals)
end
def show
set_proposal_votes(@proposal)
@commentable = @proposal
@root_comments = @proposal.comments.roots.recent.page(params[:page]).per(10).for_render
@comments = @root_comments.inject([]){|all, root| all + Comment.descendants_of(root).for_render}
@all_visible_comments = @root_comments + @comments
set_comment_flags(@all_visible_comments)
end
def new
@proposal = Proposal.new
load_featured_tags
end
def create
@proposal = Proposal.new(proposal_params)
@proposal.author = current_user
if @proposal.save_with_captcha
ahoy.track :proposal_created, proposal_id: @proposal.id
redirect_to @proposal, notice: t('flash.actions.create.notice', resource_name: 'Proposal')
else
load_featured_tags
render :new
end
end
def edit
load_featured_tags
end
def update
@proposal.assign_attributes(proposal_params)
if @proposal.save_with_captcha
redirect_to @proposal, notice: t('flash.actions.update.notice', resource_name: 'Proposal')
else
load_featured_tags
render :edit
end
end
def flag
Flag.flag(current_user, @proposal)
respond_with @proposal, template: 'proposals/_refresh_flag_actions'
end
def unflag
Flag.unflag(current_user, @proposal)
respond_with @proposal, template: 'proposals/_refresh_flag_actions'
end
def vote
@proposal.register_vote(current_user, 'yes')
set_proposal_votes(@proposal)
@@ -78,17 +22,7 @@ class ProposalsController < ApplicationController
params.require(:proposal).permit(:title, :question, :summary, :description, :external_url, :video_url, :responsible_name, :tag_list, :terms_of_service, :captcha, :captcha_key)
end
def load_featured_tags
@featured_tags = ActsAsTaggableOn::Tag.where(featured: true)
end
def parse_tag_filter
if params[:tag].present?
@tag_filter = params[:tag] if ActsAsTaggableOn::Tag.where(name: params[:tag]).exists?
end
end
def parse_search_terms
@search_terms = params[:search] if params[:search].present?
def resource_model
Proposal
end
end