Skip to content

Commit

Permalink
Add title to contents model
Browse files Browse the repository at this point in the history
  • Loading branch information
zonque committed Apr 10, 2024
1 parent a22cf02 commit 5276447
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 18 deletions.
3 changes: 2 additions & 1 deletion app/admin/content.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
ActiveAdmin.register Content do
permit_params :name, :content, :locale, :fallback
permit_params :name, :title, :content, :locale, :fallback

index do
selectable_column
column :id do |content|
link_to content.id, admin_content_path(content)
end
column :name
column :title
column :locale
column :fallback
actions
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/contents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 9 additions & 9 deletions app/models/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions db/migrate/20240410193619_add_title_to_contents.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTitleToContents < ActiveRecord::Migration[7.1]
def change
add_column :contents, :title, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions test/controllers/contents_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ 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
get privacy_url, params: { locale: locale }
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
get tos_url, params: { locale: locale }
assert_response :success

c = Content.for(:tos, locale)
assert @response.body.include?(c)
assert @response.body.include?(c.content)
end
end
end
6 changes: 3 additions & 3 deletions test/models/content_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5276447

Please sign in to comment.