diff --git a/app/controllers/content_controller.rb b/app/controllers/content_controller.rb index 595fc42b..83d1659f 100644 --- a/app/controllers/content_controller.rb +++ b/app/controllers/content_controller.rb @@ -93,13 +93,14 @@ class ContentController < ApplicationController def edit content_type_class = content_type_from_controller(self.class) @content = content_type_class.find_by(id: params[:id]) - @serialized_content = ContentSerializer.new(@content) - if @content.nil? - return redirect_to root_path, - notice: "Either this #{content_type_class.name.downcase} doesn't exist, or you don't have access to view it." + return redirect_to(root_path, + notice: "Either this #{content_type_class.name.downcase} doesn't exist, or you don't have access to view it." + ) end + @serialized_content = ContentSerializer.new(@content) + unless @content.updatable_by? current_user return redirect_to @content, notice: t(:no_do_permission) end diff --git a/app/controllers/universes_controller.rb b/app/controllers/universes_controller.rb index e515816d..f95bdd6a 100644 --- a/app/controllers/universes_controller.rb +++ b/app/controllers/universes_controller.rb @@ -6,7 +6,8 @@ class UniversesController < ContentController define_method content_type_name do @content_type = content_type_name.to_s.singularize.capitalize.constantize - @universe = Universe.find(params[:id]) + @universe = Universe.find_by(id: params[:id]) + return redirect_to(root_path, notice: "That universe doesn't exist!") unless @universe.present? @content_list = @universe.send(content_type_name) # todo just use current_user.can_view?(@universe) and/or individual filtering diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index f341616f..da35d7ef 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -28,7 +28,8 @@ class UsersController < ApplicationController content_type_name = content_type.name.downcase.pluralize.to_sym # :characters, :items, etc define_method content_type_name do @content_type = content_type - @user = User.find(params[:id]) + @user = User.find_by(id: params[:id]) + return redirect_to(root_path, notice: "This user does not exist") unless @user.present? @content_list = @user.send(content_type_name).is_public.order(:name) render :content_list diff --git a/app/models/serializers/content_serializer.rb b/app/models/serializers/content_serializer.rb index b476d511..713f64fa 100644 --- a/app/models/serializers/content_serializer.rb +++ b/app/models/serializers/content_serializer.rb @@ -52,7 +52,7 @@ class ContentSerializer label: category.label, icon: category.icon, hidden: !!category.hidden, - fields: (self.fields.select { |field| field.attribute_category_id == category.id }.map { |field| + fields: self.fields.select { |field| field.attribute_category_id == category.id }.map { |field| { id: field.name, label: field.label, @@ -65,7 +65,7 @@ class ContentSerializer value.attribute_field_id == field.id }.try(:value) || "" } - } + (old_style_link_fields[category.name].presence || [])).sort do |a, b| + }.sort do |a, b| a_value = case a[:type] when 'name' then 0 when 'universe' then 1 @@ -90,36 +90,4 @@ class ContentSerializer } } end - - # { - # 'overview': [ - # { - # id: 'children', - # label: 'Children', - # relation: 'Character', - # type: 'link', - # value: [Character, Character, Character] - # }, - # ... - # ] - # } - def old_style_link_fields - # TODO I think we can remove this method - return {} - - categories = Hash[YAML.load_file(Rails.root.join('config', 'attributes', "#{self.class_name.downcase}.yml")).map do |category_name, details| - [ - category_name.to_s, - (details[:attributes] || []).select { |field| field[:field_type] == 'link'}.map do |field| - { - id: field[:name], - label: field[:label], - type: field[:field_type].presence || 'textarea', - old_column_source: field[:name], - value: self.raw_model.send(field[:name]) - } - end - ] - end] - end end diff --git a/app/views/content/form/_panel.html.erb b/app/views/content/form/_panel.html.erb index 8e8bd933..16e128eb 100644 --- a/app/views/content/form/_panel.html.erb +++ b/app/views/content/form/_panel.html.erb @@ -44,7 +44,7 @@ valid_universes += current_user.contributable_universes else # Premium content - if current_user.on_premium_plan? || (content.persisted? && content.user == current_user) + if current_user.on_premium_plan? || (raw_model.persisted? && content.user == current_user) valid_universes += current_user.universes else show_premium_notice = true