Skip to content

Commit

Permalink
Responders implementation finished
Browse files Browse the repository at this point in the history
  • Loading branch information
miks committed Feb 15, 2015
1 parent 73a0b15 commit a9efa21
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 109 deletions.
15 changes: 12 additions & 3 deletions releaf-content/app/controllers/releaf/content/nodes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Releaf::Content
class NodesController < Releaf::BaseController
respond_to :json, only: [:create, :update, :copy, :move]

def generate_url
tmp_resource = prepare_resource
tmp_resource.name = params[:name]
Expand Down Expand Up @@ -91,19 +93,26 @@ def copy_move_common(&block)

if params[:new_parent_id].nil?
@resource.errors.add(:base, 'parent not selected')
respond_with(@resource, responder: responder_class(:after_save))
respond_with(@resource)
else
begin
@resource = yield(@resource)
rescue ActiveRecord::RecordInvalid => e
respond_with(e.record, responder: responder_class(:after_save))
respond_with(e.record)
else
resource_class.updated
respond_with(@resource, responder: responder_class(:after_save), redirect: true, location: url_for(action: :index))
respond_with(@resource, redirect: true, location: url_for(action: :index))
end
end
end

def action_responders
super.merge(
copy: Releaf::Responders::AfterSaveResponder,
move: Releaf::Responders::AfterSaveResponder
)
end

def copy_move_dialog_common
@resource = resource_class.find params[:id]
@collection = resource_class.roots
Expand Down
87 changes: 17 additions & 70 deletions releaf-core/app/controllers/releaf/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ module Releaf
class FeatureDisabled < StandardError; end

class BaseController < ActionController::Base
respond_to :html, :json
respond_to :html
respond_to :json, only: [:create, :update]
protect_from_forgery
include Releaf.access_control_module
include Releaf::Breadcrumbs
include Releaf::RichtextAttachments
include Releaf::Responders

before_filter :manage_ajax, :setup, :verify_feature_availability!

Expand All @@ -22,7 +24,6 @@ class BaseController < ActionController::Base
:controller_scope_name,
:current_params,
:current_url,
:has_template?,
:active_view,
:index_url,
:page_title,
Expand All @@ -39,12 +40,12 @@ def search text

def index
prepare_index
respond
respond_with(@collection)
end

def new
prepare_new
respond
respond_with(@resource)
end

def show
Expand All @@ -53,57 +54,36 @@ def show

def edit
prepare_edit
respond
respond_with(@resource)
end

def create
prepare_create
@resource.save
respond_with(@resource, responder: responder_class(:after_save), location: (success_url if @resource.persisted?), redirect: true)
respond_with(@resource, location: (success_url if @resource.persisted?), redirect: true)
end

def update
prepare_update
@resource.update_attributes(resource_params)
respond_with(@resource, responder: responder_class(:after_save), location: success_url)
end

def responder_class(type)
"Releaf::Responders::#{type.to_s.camelize}Responder".constantize
respond_with(@resource, location: success_url)
end

def confirm_destroy
prepare_destroy

respond_to do |format|
format.html do
unless destroyable?
@restrict_relations = list_restrict_relations
render 'refused_destroy'
end
end
end
@restricted_relations = restricted_relations unless destroyable?
respond_with(@resource, destroyable: destroyable?)
end

def toolbox
prepare_toolbox

respond_to do |format|
format.html do
render 'toolbox', locals: { resource: @resource }
end
end
respond_with(@resource)
end

def destroy
prepare_destroy

action_success = destroyable? && @resource.destroy
render_notification(action_success, failure_message_key: 'cant destroy, because relations exists')

respond_to do |format|
format.html { redirect_to index_url }
end
@resource.destroy if destroyable?
respond_with(@resource, location: index_url)
end

# Check if @resource has existing restrict relation and it can be deleted
Expand All @@ -120,7 +100,7 @@ def destroyable?
# Lists relations for @resource with dependent: :restrict_with_exception
#
# @return hash of all related objects, who have dependancy :restrict_with_exception
def list_restrict_relations
def restricted_relations
relations = {}
resource_class.reflect_on_all_associations.each do |assoc|
if assoc.options[:dependent] == :restrict_with_exception && @resource.send(assoc.name).exists?
Expand Down Expand Up @@ -202,13 +182,6 @@ def resource_class
@resource_class ||= self.class.resource_class
end

# Cheheck if there is a template in lookup_context with given name.
#
# @return `true` or `false`
def has_template? name
lookup_context.template_exists?( name, lookup_context.prefixes, false )
end

# Returns action > view translation hash
# @return Hash
def action_views
Expand Down Expand Up @@ -289,14 +262,6 @@ def controller_scope_name
@controller_scope_name ||= 'admin.' + self.class.name.sub(/Controller$/, '').underscore.gsub('/', '_')
end

def mass_assigment_actions
['create', 'update']
end

def mass_assigment_action?
mass_assigment_actions.include? params[:action]
end

def feature_available? feature
@features[feature].present?
end
Expand All @@ -319,14 +284,6 @@ def render_notification(status, success_message_key: "#{params[:action]} succeed
end
end

protected

def respond
respond_to do |format|
format.html
end
end

def prepare_index
# load resource only if they are not loaded yet
@collection = resources unless collection_given?
Expand Down Expand Up @@ -447,7 +404,6 @@ def required_params
def setup
@features = {
edit: true,
edit_ajax_reload: true,
create: true,
destroy: true,
index: true,
Expand All @@ -472,7 +428,7 @@ def permitted_params
#
# @return [String] url
def success_url
url_for( action: 'edit', id: @resource.id, index_url: index_url)
url_for(action: 'edit', id: @resource.id, index_url: index_url)
end

# returns all params except :controller, :action and :format
Expand All @@ -482,11 +438,11 @@ def current_params

def feature_disabled exception
@feature = exception.message
error_response('feature_disabled', 403)
respond_with(nil, responder: action_responder(:feature_disabled))
end

def access_denied
error_response('access_denied', 403)
respond_with(nil, responder: action_responder(:access_denied))
end

def ajax?
Expand All @@ -497,18 +453,9 @@ def layout
ajax? ? false : "releaf/admin"
end

private

def manage_ajax
@_ajax = params.has_key? :ajax
params.delete(:ajax)
end

def error_response error_page, error_status
respond_to do |format|
format.html { render "releaf/error_pages/#{error_page}", status: error_status }
format.any { render text: '', status: error_status }
end
end
end
end
4 changes: 2 additions & 2 deletions releaf-core/app/controllers/releaf/core/errors_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Releaf::Core::ErrorsController < ::Releaf::BaseController
class Releaf::Core::ErrorsController < Releaf::BaseController
def page_not_found
error_response('page_not_found', 404)
respond_with(nil, responder: action_responder(:page_not_found))
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def setup
@features = {
edit: true,
index: true,
edit_ajax_reload: true
}
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def section_body

def restricted_relations
tag(:ul, class: "block restricted-relations") do
template_variable("restrict_relations").collect do|key, relation|
template_variable("restricted_relations").collect do|key, relation|
tag(:li) do
restricted_relation(relation, key)
end
Expand Down
31 changes: 31 additions & 0 deletions releaf-core/app/lib/releaf/responders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Releaf::Responders

def respond_with(resource = nil, options = {}, &block)
options[:responder] = active_responder unless options.has_key? :responder
super
end

def action_responders
{
create: Releaf::Responders::AfterSaveResponder,
update: Releaf::Responders::AfterSaveResponder,
confirm_destroy: Releaf::Responders::ConfirmDestroyResponder,
destroy: Releaf::Responders::DestroyResponder,
access_denied: Releaf::Responders::AccessDeniedResponder,
feature_disabled: Releaf::Responders::FeatureDisabledResponder,
page_not_found: Releaf::Responders::PageNotFoundResponder,
}
end

# Returns generic view name for given action
# @return String
def action_responder(_action_name)
action_responders[_action_name.to_sym]
end

# Returns generic view name for current action
# @return String
def active_responder
action_responder(action_name)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Releaf::Responders
class AccessDeniedResponder < ActionController::Responder
include Releaf::Responders::ErrorResponder

def status_code
403
end
end
end
13 changes: 13 additions & 0 deletions releaf-core/app/lib/releaf/responders/confirm_destroy_responder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Releaf::Responders
class ConfirmDestroyResponder < ActionController::Responder
delegate :render_notification, to: :controller

def to_html
if options[:destroyable]
super
else
render 'refused_destroy'
end
end
end
end
10 changes: 10 additions & 0 deletions releaf-core/app/lib/releaf/responders/destroy_responder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Releaf::Responders
class DestroyResponder < ActionController::Responder
delegate :render_notification, to: :controller

def to_html
render_notification(@resource.destroyed?, failure_message_key: 'cant destroy, because relations exists')
super
end
end
end
9 changes: 9 additions & 0 deletions releaf-core/app/lib/releaf/responders/error_responder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Releaf::Responders::ErrorResponder
def template
self.class.name.split("::").last.gsub(/Responder$/, "").underscore
end

def to_html
render "releaf/error_pages/#{template}", status: status_code
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Releaf::Responders
class FeatureDisabledResponder < ActionController::Responder
include Releaf::Responders::ErrorResponder

def status_code
403
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Releaf::Responders
class PageNotFoundResponder < ActionController::Responder
include Releaf::Responders::ErrorResponder

def status_code
404
end
end
end
22 changes: 0 additions & 22 deletions releaf-core/spec/controllers/releaf/base_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,6 @@ def resource_class
end
class FooFormBuilder; end

describe "#mass_assigment_actions" do
it "returns create and update as mass assigment actions" do
expect(subject.mass_assigment_actions).to eq(["create", "update"])
end
end

describe "#mass_assigment_actions" do
context "when current action is mass assigment action" do
it "returns true" do
allow(subject).to receive(:params).and_return(action: "update")
expect(subject.mass_assigment_action?).to be true
end
end

context "when current action is not mass assigment action" do
it "returns false" do
allow(subject).to receive(:params).and_return(action: "edit")
expect(subject.mass_assigment_action?).to be false
end
end
end

describe "#action_views" do
it "returns action > view translation hash" do
hash = {
Expand Down
Loading

0 comments on commit a9efa21

Please sign in to comment.