diff --git a/app/controllers/concerns/paginable.rb b/app/controllers/concerns/paginable.rb index b915e81..5f2108d 100644 --- a/app/controllers/concerns/paginable.rb +++ b/app/controllers/concerns/paginable.rb @@ -1,24 +1,22 @@ module Paginable extend ActiveSupport::Concern - - #included do - # helper :paginable_renderise - #end - - # Renders paginable layout with the partial view passed - # partial {String} - Represents a path to where the partial view is stored - # controller {String} - Represents the name of the controller to handles the pagination - # action {String} - Represents the method name within the controller - # scope {ActiveRecord::Relation} - Represents scope variable - # locals {Hash} - A hash objects with any additional local variables to be passed to the partial view - def paginable_renderise(partial: nil, controller: params[:controller], action: params[:action], scope: nil, locals: {}) - raise ArgumentError, 'scope should be an instance of ActiveRecord::Relation class' unless scope.is_a?(ActiveRecord::Relation) - raise ArgumentError, 'locals should be an instance of Hash' unless locals.is_a?(Hash) - render(layout: '/layouts/paginable', partial: partial, locals: { - controller: controller, - action: action, - # The scope is paginable if it has been chained with page method from kaminari which contains methods such as total_pages - paginable: scope.respond_to?(:total_pages), - scope: scope }.merge(locals)) + + included do + # Renders paginable layout with the partial view passed + # partial {String} - Represents a path to where the partial view is stored + # controller {String} - Represents the name of the controller to handles the pagination + # action {String} - Represents the method name within the controller + # scope {ActiveRecord::Relation} - Represents scope variable + # locals {Hash} - A hash objects with any additional local variables to be passed to the partial view + def paginable_renderise(partial: nil, controller: params[:controller], action: params[:action], scope: nil, locals: {}) + raise ArgumentError, 'scope should be an instance of ActiveRecord::Relation class' unless scope.is_a?(ActiveRecord::Relation) + raise ArgumentError, 'locals should be an instance of Hash' unless locals.is_a?(Hash) + render(layout: '/layouts/paginable', partial: partial, locals: { + controller: controller, + action: action, + # The scope is paginable if it has been chained with page method from kaminari which contains methods such as total_pages + paginable: scope.respond_to?(:total_pages), + scope: scope }.merge(locals)) + end end end \ No newline at end of file diff --git a/app/controllers/paginable/themes_controller.rb b/app/controllers/paginable/themes_controller.rb new file mode 100644 index 0000000..94721a8 --- /dev/null +++ b/app/controllers/paginable/themes_controller.rb @@ -0,0 +1,13 @@ +module Paginable + class ThemesController < ApplicationController + include Paginable + # /paginable/themes/index/:page + def index + raise Pundit::NotAuthorizedError unless SuperAdmin::ThemePolicy.new(current_user).index? + themes = params[:page] == 'ALL' ? + Theme.all : + Theme.page(params[:page]) + paginable_renderise(partial: 'index', scope: themes) + end + end +end \ No newline at end of file diff --git a/app/controllers/super_admin/themes_controller.rb b/app/controllers/super_admin/themes_controller.rb new file mode 100644 index 0000000..94c13f7 --- /dev/null +++ b/app/controllers/super_admin/themes_controller.rb @@ -0,0 +1,62 @@ +module SuperAdmin + class ThemesController < ApplicationController + helper PaginableHelper + def index + authorize(Theme) + render(:index, locals: { themes: Theme.page(1) }) + end + + def new + authorize(Theme) + render(:new_edit, locals: { theme: Theme.new, options: { url: super_admin_themes_path, method: :POST, title: _('New Theme') }}) + end + + def create + authorize(Theme) + begin + pparams = permitted_params + Theme.create!(pparams) + flash[:notice] = _('Theme created successfully') + rescue ActionController::ParameterMissing + flash[:alert] = _('Unable to save since theme parameter is missing') + rescue ActiveRecord::RecordInvalid => e + flash[:alert] = e.message + end + redirect_to(action: :index) + end + + def edit + authorize(Theme) + begin + theme = Theme.find(params[:id]) + render(:new_edit, locals: { theme: theme, options: { url: super_admin_theme_path(theme), method: :PUT, title: _('Edit Theme') }}) + rescue ActiveRecord::RecordNotFound + flash[:alert] = _('There is no theme associated with id %{id}') % { :id => params[:id] } + redirect_to(action: :index) + end + end + + def update + authorize(Theme) + begin + pparams = permitted_params + Theme.find(params[:id]).update_attributes!(pparams) + flash[:notice] = _('Theme updated successfully') + rescue ActiveRecord::RecordNotFound + flash[:alert] = _('There is no theme associated with id %{id}') % { :id => params[:id] } + rescue ActionController::ParameterMissing + flash[:alert] = _('Unable to save since theme parameter is missing') + rescue ActiveRecord::RecordInvalid => e + flash[:alert] = e.message + end + redirect_to(action: :index) + end + + # Private instance methods + private + + def permitted_params + params.require(:theme).permit(:title, :description) + end + end +end diff --git a/app/policies/theme_policy.rb b/app/policies/theme_policy.rb new file mode 100644 index 0000000..d7b7895 --- /dev/null +++ b/app/policies/theme_policy.rb @@ -0,0 +1,21 @@ +class ThemePolicy < ApplicationPolicy + def initialize(user, *args) + raise Pundit::NotAuthorizedError, _("must be logged in") unless user + @user = user + end + def index? + @user.can_super_admin? + end + def new? + @user.can_super_admin? + end + def create? + @user.can_super_admin? + end + def edit? + @user.can_super_admin? + end + def update? + @user.can_super_admin? + end +end diff --git a/app/views/layouts/_branding.html.erb b/app/views/layouts/_branding.html.erb index 2b89344..e59a9b4 100644 --- a/app/views/layouts/_branding.html.erb +++ b/app/views/layouts/_branding.html.erb @@ -79,8 +79,8 @@