diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f2d027b8..57240319 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -66,4 +66,8 @@ class ApplicationController < ActionController::Base email: id.to_s + '@localhost', password: id.to_s) end + + def content_type_from_controller(content_controller_name) + content_controller_name.to_s.chomp('Controller').singularize.constantize + end end diff --git a/app/controllers/concerns/has_ownership.rb b/app/controllers/concerns/has_ownership.rb index 77695f72..13eb2b27 100644 --- a/app/controllers/concerns/has_ownership.rb +++ b/app/controllers/concerns/has_ownership.rb @@ -10,21 +10,22 @@ module HasOwnership private def redirect_path - model = self.class.to_s.chomp('Controller').singularize.constantize + model = content_type_from_controller(self.class) "#{model.to_s.downcase}_list_path" end def require_ownership - model = self.class.to_s.chomp('Controller').singularize.constantize + model = content_type_from_controller(self.class) redirect_if_not_owned model.find(params[:id]), send(redirect_path) rescue redirect_to '/' end def hide_if_private + return # todo this return if try(:privacy).try(:downcase) == 'public' - model = self.class.to_s.chomp('Controller').singularize.constantize + model = content_type_from_controller(self.class) redirect_if_private model.find(params[:id]), redirect_path rescue redirect_to '/' @@ -36,7 +37,7 @@ module HasOwnership end def redirect_if_private(object_to_check, redirect_path) - return if public? object_to_check + return if visble? object_to_check redirect_to redirect_path, notice: t(:no_view_permission) end @@ -44,9 +45,11 @@ module HasOwnership session[:user] && session[:user] == object.user.id end - def public?(object) - (owned_by_current_user? object) || \ - (object.universe && object.universe.privacy.downcase == 'public') + def visible?(object) + [ + owned_by_current_user?(object), + object.universe.try(:privacy).try(:downcase) == 'public' + ].any? end module ClassMethods diff --git a/app/controllers/content_controller.rb b/app/controllers/content_controller.rb index bd93cec2..d5a84c1f 100644 --- a/app/controllers/content_controller.rb +++ b/app/controllers/content_controller.rb @@ -2,4 +2,108 @@ class ContentController < ApplicationController include HasOwnership before_action :create_anonymous_account_if_not_logged_in, only: [:edit, :create, :update] + + def index + @content = content_type_from_controller(self.class) + .where(user_id: session[:user]) + .order(:name) + .presence || [] + + respond_to do |format| + format.html # index.html.erb + format.json { render json: @content } + end + end + + def show + @content = content_type_from_controller(self.class) + .find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.json { render json: @content } + end + end + + def new + @content = content_type_from_controller(self.class) + .new + + respond_to do |format| + format.html # new.html.erb + format.json { render json: @content } + end + end + + def edit + @content = content_type_from_controller(self.class) + .find(params[:id]) + end + + def create + content_type = content_type_from_controller(self.class) + + @content = content_type + .new(content_params) + .tap do |c| + c.user_id = session[:user] + c.universe = universe_from_params + end + + respond_to do |format| + if @content.save + notice = t :create_success, model_name: content_type.model_name.human + format.html { redirect_to @content, notice: notice } + format.json { render json: @content, status: :created, location: @content } + else + format.html { render action: 'new' } + format.json { render json: @content.errors, status: :unprocessable_entity } + end + end + end + + def update + content_type = content_type_from_controller(self.class) + + @content = content_type.find(params[:id]) + + respond_to do |format| + if @content.update_attributes(content_params) + notice = t :update_success, model_name: content_type.model_name.human + format.html { redirect_to @content, notice: notice } + format.json { head :no_content } + else + format.html { render action: 'edit' } + format.json { render json: @content.errors, status: :unprocessable_entity } + end + end + end + + def destroy + content_type = content_type_from_controller(self.class) + @content = content_type.find(params[:id]) + @content.destroy + + respond_to do |format| + notice = t :delete_success, model_name: content_type.model_name.human + format.html { redirect_to send("#{@content.class.to_s.downcase}_list_path"), notice: notice } + format.json { head :no_content } + end + end + + + private + + # Override in content classes + def content_params + params + end + + def universe_from_params + Universe.where(user_id: session[:user], name: params[content_symbol][:universe].strip).first + end + + def content_symbol + content_type_from_controller(self.class).to_s.downcase.to_sym + end end diff --git a/app/controllers/magic_controller.rb b/app/controllers/magic_controller.rb index fbcdf6ef..258b6e20 100644 --- a/app/controllers/magic_controller.rb +++ b/app/controllers/magic_controller.rb @@ -1,80 +1,9 @@ # Controller for the Magic model class MagicController < ContentController - def index - @magics = Magic.where(user_id: session[:user]).order(:name).presence || [] - - respond_to do |format| - format.html # index.html.erb - format.json { render json: @magics } - end - end - - def show - @magic = Magic.find(params[:id]) - - respond_to do |format| - format.html # show.html.erb - format.json { render json: @magic } - end - end - - def new - @magic = Magic.new - - respond_to do |format| - format.html # new.html.erb - format.json { render json: @magic } - end - end - - def edit - @magic = Magic.find(params[:id]) - end - - def create - @magic = create_magic_from_params - - respond_to do |format| - if @magic.save - notice = t :create_success, model_name: Magic.model_name.human - format.html { redirect_to @magic, notice: notice } - format.json { render json: @magic, status: :created, location: @magic } - else - format.html { render action: 'new' } - format.json { render json: @magic.errors, status: :unprocessable_entity } - end - end - end - - def update - @magic = Magic.find(params[:id]) - - respond_to do |format| - if @magic.update_attributes(magic_params) - notice = t :update_success, model_name: Magic.model_name.human - format.html { redirect_to @magic, notice: notice } - format.json { head :no_content } - else - format.html { render action: 'edit' } - format.json { render json: @magic.errors, status: :unprocessable_entity } - end - end - end - - def destroy - @magic = Magic.find(params[:id]) - @magic.destroy - - respond_to do |format| - format.html { redirect_to magic_list_url } - format.json { head :no_content } - end - end - private - def magic_params + def content_params params.require(:magic).permit( :universe_id, :user_id, :name, :type_of, @@ -84,16 +13,4 @@ class MagicController < ContentController :resource, :skill_level, :limitations, :notes, :private_notes) end - - def create_magic_from_params - magic = Magic.new(magic_params) - magic.user_id = session[:user] - magic.universe = universe_from_magic_params - magic - end - - def universe_from_magic_params - Universe.where(user_id: session[:user], - name: params[:magic][:universe].strip).first - end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 9eae2e1d..929f067e 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -11,14 +11,10 @@ module ApplicationHelper end def find_by_name_and_type(name, type, userid) - model = find_model_by_type type + model = type.titleize.constantize unless type.blank? model.where(name: name, user_id: userid).first unless model.nil? end - def find_model_by_type(type) - type.titleize.constantize unless type.blank? - end - def print_property(title, value, type = '') return unless value.present? diff --git a/app/views/magic/_list.html.erb b/app/views/magic/_list.html.erb index 54a2a8f5..d5c13fdf 100644 --- a/app/views/magic/_list.html.erb +++ b/app/views/magic/_list.html.erb @@ -9,9 +9,9 @@
- <% @magics.each do |magic| %> + <% @content.each do |magic| %>