Skip to content

Commit

Permalink
Add an Alchemy::EssenceHeadline
Browse files Browse the repository at this point in the history
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
mamhoff committed Apr 8, 2021
1 parent 9ec35e5 commit e50e2ac
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 1 deletion.
43 changes: 43 additions & 0 deletions app/models/alchemy/essence_headline.rb
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
1 change: 1 addition & 0 deletions app/views/alchemy/essences/_essence_headline_view.html.erb
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 db/migrate/20210406093436_add_alchemy_essence_headlines.rb
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
10 changes: 9 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_03_26_105046) do
ActiveRecord::Schema.define(version: 2021_04_06_093436) do

create_table "alchemy_attachments", force: :cascade do |t|
t.string "name"
Expand Down Expand Up @@ -80,6 +80,14 @@
t.index ["attachment_id"], name: "index_alchemy_essence_files_on_attachment_id"
end

create_table "alchemy_essence_headlines", force: :cascade do |t|
t.text "body"
t.integer "level"
t.integer "size"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

create_table "alchemy_essence_htmls", force: :cascade do |t|
t.text "source"
end
Expand Down
69 changes: 69 additions & 0 deletions spec/models/alchemy/essence_headline_spec.rb
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

0 comments on commit e50e2ac

Please sign in to comment.