diff --git a/app/admin/content.rb b/app/admin/content.rb index f7ef907..55a429a 100644 --- a/app/admin/content.rb +++ b/app/admin/content.rb @@ -1,5 +1,5 @@ ActiveAdmin.register Content do - permit_params :name, :content, :locale, :fallback + permit_params :name, :title, :content, :locale, :fallback index do selectable_column @@ -7,6 +7,7 @@ link_to content.id, admin_content_path(content) end column :name + column :title column :locale column :fallback actions diff --git a/app/controllers/contents_controller.rb b/app/controllers/contents_controller.rb index 8a7359c..d13b8d7 100644 --- a/app/controllers/contents_controller.rb +++ b/app/controllers/contents_controller.rb @@ -3,7 +3,15 @@ class ContentsController < ApplicationController def method_missing(method, *args, &block) markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, extensions = {}) - @content = markdown.render(Content.for(args.first, I18n.locale)) + c = Content.for(args.first, I18n.locale) + + unless c.nil? + @content = markdown.render(c.content) + @title.push c.title + else + @content = "" + end + render :show end diff --git a/app/models/content.rb b/app/models/content.rb index 3758502..cf5f45e 100644 --- a/app/models/content.rb +++ b/app/models/content.rb @@ -2,18 +2,18 @@ class Content < ApplicationRecord def self.for(name, locale) r = where(name: name, locale: locale) if r.any? - r.first.content - else - r = where(name: name, fallback: true) - if r.any? - r.first.content - else - '' - end + return r.first end + + r = where(name: name, fallback: true) + if r.any? + return r.first + end + + return nil end def self.ransackable_attributes(auth_object = nil) - ["content", "created_at", "fallback", "id", "id_value", "locale", "name", "updated_at"] + ["content", "created_at", "fallback", "id", "id_value", "locale", "name", "title", "updated_at"] end end diff --git a/db/migrate/20240410193619_add_title_to_contents.rb b/db/migrate/20240410193619_add_title_to_contents.rb new file mode 100644 index 0000000..53fdfd5 --- /dev/null +++ b/db/migrate/20240410193619_add_title_to_contents.rb @@ -0,0 +1,5 @@ +class AddTitleToContents < ActiveRecord::Migration[7.1] + def change + add_column :contents, :title, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 03d3f2f..124ffa6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_03_17_190259) do +ActiveRecord::Schema[7.1].define(version: 2024_04_10_193619) do create_table "active_admin_comments", force: :cascade do |t| t.string "namespace" t.text "body" @@ -43,6 +43,7 @@ t.text "content" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "title" end create_table "entries", id: :string, force: :cascade do |t| diff --git a/test/controllers/contents_controller_test.rb b/test/controllers/contents_controller_test.rb index 4701e53..58f8a80 100644 --- a/test/controllers/contents_controller_test.rb +++ b/test/controllers/contents_controller_test.rb @@ -7,7 +7,7 @@ class ContentsControllerTest < ActionDispatch::IntegrationTest assert_response :success c = Content.for(:imprint, locale) - assert @response.body.include?(c) + assert @response.body.include?(c.content) end define_method("test_should_get_privacy_#{locale}") do @@ -15,7 +15,7 @@ class ContentsControllerTest < ActionDispatch::IntegrationTest assert_response :success c = Content.for(:privacy, locale) - assert @response.body.include?(c) + assert @response.body.include?(c.content) end define_method("test_should_get_tos_#{locale}") do @@ -23,7 +23,7 @@ class ContentsControllerTest < ActionDispatch::IntegrationTest assert_response :success c = Content.for(:tos, locale) - assert @response.body.include?(c) + assert @response.body.include?(c.content) end end end diff --git a/test/models/content_test.rb b/test/models/content_test.rb index f9846ec..fe60f70 100644 --- a/test/models/content_test.rb +++ b/test/models/content_test.rb @@ -2,8 +2,8 @@ class ContentTest < ActiveSupport::TestCase test "fallback" do - assert_equal contents(:tos_en).content, Content.for('tos', 'en') - assert_equal contents(:tos_en).content, Content.for('tos', 'xx') - assert_equal contents(:tos_de).content, Content.for('tos', 'de') + assert_equal contents(:tos_en).content, Content.for('tos', 'en').content + assert_equal contents(:tos_en).content, Content.for('tos', 'xx').content + assert_equal contents(:tos_de).content, Content.for('tos', 'de').content end end