-
-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds an Alchemy::EssenceHeadline. It has the following features: When used as a preview text, it will show the level of the headline so that content editors can see what levels they are using. There are is also an optional `level` element. Headlines can be configured through their content settings. One can restrict the sizes and levels available to an element, by specifying them like follows: ```yml - name: MyComponent contents: - name: my_headline type: EssenceHeadline settings: levels: [3, 4] sizes: [3] ``` If, as in the previous example, `sizes` only has one option, the select input in the element editor will not be displayed. If `sizes` is unconfigured, the default will be an empty array, and the essence will have no size class.
- Loading branch information
Showing
6 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
class EssenceHeadline < BaseRecord | ||
acts_as_essence( | ||
ingredient_column: :body, | ||
preview_text_method: :preview_text | ||
) | ||
|
||
after_initialize :set_level_and_size | ||
|
||
def preview_text(maxlength = 30) | ||
"H#{level}: #{body}"[0..maxlength - 1] | ||
end | ||
|
||
def level_options | ||
levels.map { |level| ["H#{level}", level] } | ||
end | ||
|
||
def size_options | ||
sizes.map { |size| ["H#{size}", size] } | ||
end | ||
|
||
private | ||
|
||
def content_settings | ||
content&.settings || {} | ||
end | ||
|
||
def levels | ||
content_settings.fetch(:levels, (1..6)) | ||
end | ||
|
||
def sizes | ||
content_settings.fetch(:sizes, []) | ||
end | ||
|
||
def set_level_and_size | ||
self.level ||= levels.first | ||
self.size ||= sizes.first | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= content_tag "h#{content.essence.level}", content.essence.ingredient, class: "h#{content.essence.size} #{options[:classes]}" %> |
12 changes: 12 additions & 0 deletions
12
db/migrate/20210406093436_add_alchemy_essence_headlines.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddAlchemyEssenceHeadlines < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table :alchemy_essence_headlines do |t| | ||
t.text :body | ||
t.integer :level | ||
t.integer :size | ||
t.timestamps | ||
end | ||
end | ||
end |
1 change: 1 addition & 0 deletions
1
spec/dummy/db/migrate/20210406093436_add_alchemy_essence_headlines.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../db/migrate/20210406093436_add_alchemy_essence_headlines.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Alchemy::EssenceHeadline do | ||
subject(:essence) do | ||
Alchemy::EssenceHeadline.new( | ||
body: ingredient_value, | ||
level: 2, | ||
size: 3 | ||
) | ||
end | ||
|
||
let(:ingredient_value) { "A headline" } | ||
|
||
it_behaves_like "an essence" | ||
|
||
describe "#level_options" do | ||
subject { essence.level_options } | ||
|
||
it { is_expected.to eq([["H1", 1], ["H2", 2], ["H3", 3], ["H4", 4], ["H5", 5], ["H6", 6]]) } | ||
|
||
context "when restricted through the essence settings" do | ||
before do | ||
expect(essence).to receive_message_chain(:content, :settings).and_return(levels: [2, 3]) | ||
end | ||
|
||
it { is_expected.to eq([["H2", 2], ["H3", 3]]) } | ||
end | ||
end | ||
|
||
describe "#size_options" do | ||
subject { essence.size_options } | ||
|
||
it { is_expected.to eq([]) } | ||
|
||
context "when enabled through the essence settings" do | ||
before do | ||
expect(essence).to receive_message_chain(:content, :settings).and_return(sizes: [3, 4]) | ||
end | ||
|
||
it { is_expected.to eq([["H3", 3], ["H4", 4]]) } | ||
end | ||
end | ||
|
||
describe "initialization" do | ||
describe "level" do | ||
subject { Alchemy::EssenceHeadline.new.level } | ||
|
||
it { is_expected.to eq(1) } | ||
end | ||
|
||
describe "size" do | ||
let(:content) { build(:alchemy_content) } | ||
let(:essence) { Alchemy::EssenceHeadline.new(content: content) } | ||
subject { essence.size } | ||
|
||
it { is_expected.to be_nil } | ||
|
||
context "when enabled through the essence settings" do | ||
before do | ||
expect(content).to receive(:settings).and_return(sizes: [3, 4]).twice | ||
end | ||
|
||
it { is_expected.to eq(3) } | ||
end | ||
end | ||
end | ||
end |