-
-
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.
Merge pull request #2060 from mamhoff/alchemy-essence-headline
Add a EssenceHeadline
- Loading branch information
Showing
11 changed files
with
207 additions
and
4 deletions.
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
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,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
class EssenceHeadline < BaseRecord | ||
acts_as_essence | ||
|
||
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 |
36 changes: 36 additions & 0 deletions
36
app/views/alchemy/essences/_essence_headline_editor.html.erb
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,36 @@ | ||
<%= content_tag :div, | ||
id: essence_headline_editor.dom_id, | ||
class: essence_headline_editor.css_classes, | ||
data: essence_headline_editor.data_attributes do %> | ||
<%= content_label(essence_headline_editor) %> | ||
<%= text_field_tag( | ||
essence_headline_editor.form_field_name, | ||
essence_headline_editor.essence.body, | ||
id: essence_headline_editor.form_field_id | ||
) %> | ||
|
||
<div class="input-row"> | ||
<% essence = essence_headline_editor.essence %> | ||
<% if essence.level_options.length > 1 %> | ||
<div class="input-column"> | ||
<%= label_tag Alchemy::EssenceHeadline.human_attribute_name(:level) %> | ||
<%= select_tag( | ||
essence_headline_editor.form_field_name(:level), | ||
options_for_select(essence_headline_editor.essence.level_options, essence_headline_editor.essence.level), | ||
class: "alchemy_selectbox full_width" | ||
) %> | ||
</div> | ||
<% end %> | ||
|
||
<% if essence.size_options.length > 1 %> | ||
<div class="input-column"> | ||
<%= label_tag Alchemy::EssenceHeadline.human_attribute_name(:size) %> | ||
<%= select_tag( | ||
essence_headline_editor.form_field_name(:size), | ||
options_for_select(essence_headline_editor.essence.size_options, essence_headline_editor.essence.size), | ||
class: "alchemy_selectbox full_width" | ||
) %> | ||
</div> | ||
<% end %> | ||
</div> | ||
<% 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,9 @@ | ||
<%- html_options = local_assigns.fetch(:html_options, {}) -%> | ||
|
||
<%= content_tag "h#{content.essence.level}", | ||
content.essence.ingredient, | ||
class: [ | ||
content.essence.size ? "h#{content.essence.size}" : nil, | ||
html_options[:class] | ||
] | ||
%> |
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
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 |
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
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
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,90 @@ | ||
# 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 | ||
|
||
describe "creating from a content" do | ||
it "should have the size and level fields filled with correct defaults" do | ||
element = create(:alchemy_element) | ||
|
||
allow(element).to receive(:content_definition_for) do | ||
{ | ||
"name" => "headline", | ||
"type" => "EssenceHeadline", | ||
"settings" => { | ||
"sizes" => [3], | ||
"levels" => [2, 3], | ||
}, | ||
}.with_indifferent_access | ||
end | ||
|
||
content = Alchemy::Content.create(element: element, name: "headline") | ||
expect(content.essence.size).to eq(3) | ||
expect(content.essence.level).to eq(2) | ||
end | ||
end | ||
end |