mirror of
https://github.com/indentlabs/notebook.git
synced 2025-10-26 11:19:22 +00:00
Abstract core content functionality from magic controller to content controller
This commit is contained in:
parent
b05c15bbbd
commit
d6d10a30cc
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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?
|
||||
|
||||
|
||||
@ -9,9 +9,9 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @magics.each do |magic| %>
|
||||
<% @content.each do |magic| %>
|
||||
<tr>
|
||||
<td style="width: 160px;"><%= simple_format link_to magic.name, magic_path(magic) %></td>
|
||||
<td style="width: 160px;"><%= simple_format link_to magic.name, magic %></td>
|
||||
<td><%= simple_format magic.type_of %></td>
|
||||
<td style="width: 240px;">
|
||||
<% if session[:user] and session[:user] == magic.user.id %>
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
<%- model_class = @magic.class -%>
|
||||
<%- model_class = @content.class -%>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-xs-3">
|
||||
@ -20,8 +20,8 @@
|
||||
|
||||
<div class="col-xs-8">
|
||||
<div class="card">
|
||||
<h1 class="card-heading">Editing <%= @magic.name %></h1>
|
||||
<%= form_for @magic,
|
||||
<h1 class="card-heading">Editing <%= @content.name %></h1>
|
||||
<%= form_for @content,
|
||||
:url => magic_edit_process_path,
|
||||
:html => { :class => 'form-horizontal' } do |form| %>
|
||||
<div class="card-body">
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
<%- model_class = Magic.new.class -%>
|
||||
<% if @magics.length > 0 %>
|
||||
<%- model_class = @content.class -%>
|
||||
<% if @content.length > 0 %>
|
||||
<div class="card">
|
||||
<h1 class="card-heading">
|
||||
Magic
|
||||
@ -8,7 +8,7 @@
|
||||
<% end %>
|
||||
</h1>
|
||||
|
||||
<%= render 'list' %>
|
||||
<%= render 'list', locals: { content: @content } %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
<div class="col-md-9">
|
||||
<div class="card">
|
||||
|
||||
<%= form_for @magic,
|
||||
<%= form_for @content,
|
||||
:url => magic_create_process_path,
|
||||
:html => { :class => 'form-horizontal' } do |form| %>
|
||||
|
||||
@ -23,7 +23,5 @@
|
||||
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
@ -1,4 +1,5 @@
|
||||
<%- model_class = @magic.class -%>
|
||||
<%- model_class = @content.class -%>
|
||||
<% @magic = @content %>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-xs-3">
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
|
||||
en:
|
||||
hello: Hello world
|
||||
|
||||
|
||||
quotes:
|
||||
- text: >
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer
|
||||
@ -38,6 +38,7 @@ en:
|
||||
|
||||
create_success: "%{model_name} was successfully created."
|
||||
update_success: "%{model_name} was successfully updated."
|
||||
delete_success: "%{model_name} was successfully deleted."
|
||||
|
||||
location_create_upload_map_error: >
|
||||
Location was created, but your map did not upload. Please try again.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user