PageRenderTime 1193ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/cd2_cms/application_controller.rb

https://gitlab.com/cd2/cms
Ruby | 113 lines | 90 code | 21 blank | 2 comment | 11 complexity | 32b5043cde056af7b9c1491b89051a81 MD5 | raw file
  1. require_dependency 'cd2_cms/concerns/show_page'
  2. module Cd2Cms
  3. class ApplicationController < ActionController::Base
  4. protect_from_forgery with: :exception
  5. #authorize_resource
  6. #check_authorization :unless => :devise_controller?
  7. before_action :authenticate_user!
  8. before_action unless: :devise_controller? do
  9. set_resource_driver
  10. set_paper_trail_whodunnit
  11. set_ransack_search_param
  12. set_links
  13. end
  14. before_action :set_object, only: [:show, :edit, :update, :destroy], unless: :devise_controller?
  15. layout :layout
  16. if Rails.env.development?
  17. before_action do
  18. if ENV['USER'] == 'henry' && !signed_in?
  19. sign_in(:user, Cd2Cms::User.first)
  20. end
  21. end
  22. end
  23. def logs
  24. PaperTrail::Version.all
  25. end
  26. def index
  27. if @resource_driver.respond_to? :ordered
  28. @q = @resource_driver.try(:ordered).ransack(params[:q])
  29. else
  30. @q = @resource_driver.ransack(params[:q])
  31. end
  32. @objects = @q.result(distinct: true)
  33. end
  34. def show
  35. end
  36. def new
  37. @object = @resource_driver.new
  38. end
  39. def create
  40. @object = @resource_driver.new(object_params)
  41. if (params[:commit] == 'upload')
  42. render :new
  43. elsif @object.save
  44. @object.assign_author(current_user) if @object.class.included_modules.include? Cd2Cms::SitePage
  45. flash[:notice] = "#{controller_name.classify} was created"
  46. redirect_to @index_path
  47. else
  48. render :new
  49. end
  50. end
  51. def edit
  52. end
  53. def update
  54. if (params[:commit] == 'upload')
  55. @object.assign_attributes(object_params)
  56. render :edit
  57. elsif @object.update(object_params)
  58. flash[:notice] = "The #{controller_name.classify} was updated"
  59. redirect_to @index_path
  60. else
  61. render :edit
  62. end
  63. end
  64. def destroy
  65. @object.destroy
  66. flash[:success] = "#{controller_name.classify} was destroyed"
  67. redirect_to @index_path
  68. end
  69. def sort
  70. @resource_driver.update_order(params[@resource_driver.model_name.param_key])
  71. respond_to do |format|
  72. format.html { redirect_to @index_path if @index_path }
  73. format.js { head :ok, content_type: "text/html" }
  74. end
  75. end
  76. def confirm_destroy
  77. end
  78. private
  79. def set_ransack_search_param
  80. @search_param = 'name'
  81. end
  82. def object_params
  83. params.require(@resource_driver.model_name.param_key).permit(@resource_driver.permitted_params)
  84. end
  85. def layout
  86. devise_controller? ? "cd2_cms/devise_application" : "cd2_cms/application"
  87. end
  88. def current_ability
  89. @current_ability ||= Ability.new(current_user)
  90. end
  91. end
  92. end