Skip to content

Commit

Permalink
Remove local options from essence editors
Browse files Browse the repository at this point in the history
Passing options as locals to essence editors is not possible anymore.

Static values should be set on the content settings on the contents
defintion in elements.yml instead. For dynamic values please use a
custom essence class.
  • Loading branch information
tvdeyen committed Oct 21, 2019
1 parent 46d9682 commit f0ed7a0
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 97 deletions.
32 changes: 9 additions & 23 deletions app/helpers/alchemy/admin/essences_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,8 @@ module EssencesHelper
include Alchemy::Admin::ContentsHelper

# Renders the Content editor partial from the given Content.
# For options see -> render_essence
# @deprecated
def render_essence_editor(content, options = {}, html_options = {})
if !options.empty?
Alchemy::Deprecation.warn <<~WARN
Passing options to `render_essence_editor` is deprecated and will be removed from Alchemy 5.0.
Add your static `#{options.keys}` options to the `#{content.name}` content definitions `settings` of `#{content.element&.name}` element.
For dynamic options consider adding your own essence class.
WARN
end
if !html_options.empty?
Alchemy::Deprecation.warn <<~WARN
Passing html_options to `render_essence_editor` is deprecated and will be removed in Alchemy 5.0 without replacement.
WARN
end
render_essence(content, :editor, {for_editor: options}, html_options)
def render_essence_editor(content)
render_essence(content, :editor)
end
deprecate :render_essence_editor, deprecator: Alchemy::Deprecation

Expand All @@ -32,16 +18,16 @@ def render_essence_editor(content, options = {}, html_options = {})
# Content creation on the fly:
#
# If you update the elements.yml file after creating an element this helper displays a error message with an option to create the content.
# @deprecated
def render_essence_editor_by_name(element, name, options = {}, html_options = {})
#
def render_essence_editor_by_name(element, name)
if element.blank?
return warning('Element is nil', Alchemy.t(:no_element_given))
end
content = element.content_by_name(name)
if content.nil?
render_missing_content(element, name, options)
render_missing_content(element, name)
else
render_essence_editor(content, options, html_options)
render_essence_editor(content)
end
end
deprecate :render_essence_editor_by_name, deprecator: Alchemy::Deprecation
Expand All @@ -68,9 +54,9 @@ def pages_for_select(pages = nil, selected = nil, prompt = "Choose page", page_a
end

# Renders the missing content partial
# @deprecated
def render_missing_content(element, name, options)
render 'alchemy/admin/contents/missing', {element: element, name: name, options: options}
#
def render_missing_content(element, name)
render 'alchemy/admin/contents/missing', {element: element, name: name}
end
deprecate :render_missing_content, deprecator: Alchemy::Deprecation

Expand Down
4 changes: 2 additions & 2 deletions app/helpers/alchemy/elements_block_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def essence(name)
# Block-level helper class for element editors.
# @deprecated
class ElementEditorHelper < BlockHelper
def edit(name, *args)
helpers.render_essence_editor_by_name(element, name.to_s, *args)
def edit(name)
helpers.render_essence_editor_by_name(element, name.to_s)
end
deprecate :edit, deprecator: Alchemy::Deprecation
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/alchemy/admin/essence_files/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% css_classes = @content.settings_value(:css_classes, local_assigns.fetch(:options, {})) %>
<% css_classes = @content.settings[:css_classes] %>

<%= alchemy_form_for [:admin, @essence_file] do |f| %>
<%= f.input :link_text %>
Expand Down
7 changes: 1 addition & 6 deletions app/views/alchemy/essences/_essence_boolean_editor.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<div class="content_editor essence_boolean" id="<%= content.dom_id %>" data-content-id="<%= content.id %>">
<input type="hidden" value="0" name="<%= content.form_field_name %>">
<%= check_box_tag content.form_field_name, 1,
content.ingredient.presence || content.settings_value(
:default_value, local_assigns.fetch(:options, {})
),
class: local_assigns.fetch(:html_options, {})[:class],
style: local_assigns.fetch(:html_options, {})[:style] %>
<%= check_box_tag content.form_field_name, 1, content.ingredient %>
<label for="<%= content.form_field_id %>" style="display: inline">
<%= render_content_name(content) %>
</label>
Expand Down
2 changes: 1 addition & 1 deletion app/views/alchemy/essences/_essence_date_editor.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
content.essence, :date, {
name: content.form_field_name,
id: content.form_field_id,
value: content.settings_value(:default_value, local_assigns.fetch(:options, {}))
value: content.ingredient
}
) %>
<label for="<%= content.form_field_id %>" class="essence_date--label">
Expand Down
10 changes: 3 additions & 7 deletions app/views/alchemy/essences/_essence_file_editor.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<% dialog_link = link_to_dialog render_icon(:file, style: 'regular'),
alchemy.admin_attachments_path(
content_id: content.id,
only: content.settings_value(:only, local_assigns.fetch(:options, {})),
except: content.settings_value(:except, local_assigns.fetch(:options, {})),
options: local_assigns.fetch(:options, {})
only: content.settings[:only],
except: content.settings[:except]
),
{
title: Alchemy.t(:assign_file),
Expand All @@ -30,10 +29,7 @@
<div class="essence_file_tools">
<%= dialog_link %>
<%= link_to_dialog render_icon(:edit),
alchemy.edit_admin_essence_file_path(
id: content.essence.id,
options: local_assigns.fetch(:options, {})
),
alchemy.edit_admin_essence_file_path(id: content.essence.id),
{
title: Alchemy.t(:edit_file_properties),
size: '400x215'
Expand Down
5 changes: 2 additions & 3 deletions app/views/alchemy/essences/_essence_picture_editor.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<div class="picture_image">
<div class="thumbnail_background">
<%- if content.ingredient -%>
<%= essence_picture_thumbnail(content, options) %>
<%= essence_picture_thumbnail(content) %>
<% else %>
<%= render_icon(:image, style: 'regular') %>
<% end %>
Expand All @@ -24,8 +24,7 @@
<%- end -%>
<div class="edit_images_bottom">
<%= render 'alchemy/essences/shared/essence_picture_tools', {
content: content,
options: options
content: content
} %>
</div>
</div>
Expand Down
13 changes: 5 additions & 8 deletions app/views/alchemy/essences/_essence_select_editor.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
<% select_values = content.settings_value(:select_values, local_assigns.fetch(:options, {})) %>
<% inline = content.settings_value(:display_inline, local_assigns.fetch(:options, {})) %>
<% html_options = local_assigns.fetch(:html_options, {}) %>
<% select_values = content.settings[:select_values] %>

<%= content_tag :div,
id: content.dom_id,
class: [
"content_editor",
"essence_select",
inline ? 'display_inline' : nil
content.settings[:display_inline] ? 'display_inline' : nil
].compact, data: {content_id: content.id} do %>
<%= content_label(content) %>

<% if select_values.nil? %>
<%= warning(':select_values is nil',
"<strong>No select values given.</strong>
<br>Please provide :<code>select_values</code> either as argument to
<code>render_essence_editor</code> helper or as setting on the content definition in
<br>Please provide <code>select_values</code> on the
content definition <code>settings</code> in
<code>elements.yml</code>.") %>
<% else %>
<%
Expand All @@ -25,8 +23,7 @@
options_tags = options_for_select(select_values, content.ingredient)
end %>
<%= select_tag content.form_field_name, options_tags, {
class: ["alchemy_selectbox", "essence_editor_select", html_options[:class]].compact,
style: html_options[:style]
class: ["alchemy_selectbox", "essence_editor_select"]
} %>
<% end %>
<% end %>
10 changes: 3 additions & 7 deletions app/views/alchemy/essences/_essence_text_editor.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
<% options = local_assigns.fetch(:options, {}) %>
<% html_options = local_assigns.fetch(:html_options, {}) %>

<div class="essence_text content_editor<%= options[:display_inline].to_s == 'true' ? ' display_inline' : '' %>" id="<%= content.dom_id %>">
<div class="essence_text content_editor<%= content.settings[:display_inline] ? ' display_inline' : '' %>" id="<%= content.dom_id %>">
<%= content_label(content) %>
<%= text_field_tag(
content.form_field_name,
content.ingredient,
class: ["thin_border #{content.settings[:linkable] ? ' text_with_icon' : ''}", html_options[:class]].join(' '),
style: html_options[:style],
type: content.settings_value(:input_type) || "text"
class: "thin_border #{content.settings[:linkable] ? ' text_with_icon' : ''}",
type: content.settings[:input_type] || "text"
) %>
<% if content.settings[:linkable] %>
<%= hidden_field_tag content.form_field_name(:link), content.essence.link %>
Expand Down
17 changes: 6 additions & 11 deletions app/views/alchemy/essences/shared/_essence_picture_tools.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<% linkable = content.settings_value(:linkable, options) != false %>
<% linkable = content.settings[:linkable] != false %>

<% if content.essence && content.essence.allow_image_cropping?(options) %>
<% if content.essence && content.essence.allow_image_cropping? %>
<%= link_to_dialog render_icon(:crop),
alchemy.crop_admin_essence_picture_path(
content.essence,
options: content.settings.update(options)
), {
alchemy.crop_admin_essence_picture_path(content.essence), {
size: "1080x615",
title: Alchemy.t('Edit Picturemask'),
image_loader: false,
Expand All @@ -19,8 +16,7 @@
alchemy.admin_pictures_path(
element_id: content.element,
content_id: content.id,
swap: true,
options: options
swap: true
),
{
title: (content.ingredient ? Alchemy.t(:swap_image) : Alchemy.t(:insert_image)),
Expand Down Expand Up @@ -51,9 +47,8 @@
<%= link_to_dialog render_icon(:edit),
alchemy.edit_admin_essence_picture_path(
id: content.essence.id,
content_id: content.id,
options: options
content_id: content.id
), {
title: Alchemy.t(:edit_image_properties),
size: edit_picture_dialog_size(content, options)
size: edit_picture_dialog_size(content)
}, title: Alchemy.t(:edit_image_properties) %>
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<%%#
Available locals:
* content (The object the essence is linked to the element)
* html_options

Please consult Alchemy::Content.rb docs for further methods on the content object
%>
Expand Down
4 changes: 2 additions & 2 deletions spec/helpers/alchemy/elements_block_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ module Alchemy

describe '#edit' do
it "should delegate to the render_essence_editor_by_name helper" do
expect(scope).to receive(:render_essence_editor_by_name).with(element, "title", foo: 'bar')
subject.edit :title, foo: 'bar'
expect(scope).to receive(:render_essence_editor_by_name).with(element, "title")
subject.edit(:title)
end
end
end
Expand Down
30 changes: 19 additions & 11 deletions spec/views/essences/essence_boolean_editor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,36 @@
require 'rails_helper'

describe 'alchemy/essences/_essence_boolean_editor' do
let(:essence) { Alchemy::EssenceBoolean.new(ingredient: false) }
let(:content) { Alchemy::Content.new(essence: essence, name: 'Boolean') }
let(:element) { create(:alchemy_element, name: 'all_you_can_eat') }
let(:content) { Alchemy::Content.create(name: 'essence_boolean', type: 'EssenceBoolean', element: element) }

let(:content_definition) do
{
name: 'essence_boolean',
type: 'EssenceBoolean'
}.with_indifferent_access
end

before do
expect(element).to receive(:content_definition_for) { content_definition }
allow_any_instance_of(Alchemy::Content).to receive(:definition) { content_definition }
allow(view).to receive(:render_content_name).and_return(content.name)
allow(view).to receive(:render_hint_for).and_return('')
end

it "renders a checkbox" do
it "renders an unchecked checkbox" do
render partial: "alchemy/essences/essence_boolean_editor", locals: {content: content}
expect(rendered).to have_selector('input[type="checkbox"]')
end

context 'with default value given in view local options' do
it "checks the checkbox" do
render partial: "alchemy/essences/essence_boolean_editor", locals: {content: content, options: {default_value: true}}
expect(rendered).to have_selector('input[type="checkbox"][checked="checked"]')
end
end

context 'with default value given in content settings' do
before { allow(content).to receive(:settings).and_return({default_value: true}) }
let(:content_definition) do
{
name: 'essence_boolean',
type: 'EssenceBoolean',
default: true
}.with_indifferent_access
end

it "checks the checkbox" do
render partial: "alchemy/essences/essence_boolean_editor", locals: {content: content}
Expand Down
18 changes: 18 additions & 0 deletions spec/views/essences/essence_date_editor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'alchemy/essences/_essence_date_editor' do
let(:content) { Alchemy::Content.new(essence: essence) }
let(:essence) { Alchemy::EssenceDate.new }

before do
view.class.send(:include, Alchemy::Admin::BaseHelper)
allow(view).to receive(:content_label).and_return(content.name)
end

it "renders a datepicker" do
render 'alchemy/essences/essence_date_editor', content: content
expect(rendered).to have_css('input[type="text"][data-datepicker-type="date"].date')
end
end
11 changes: 7 additions & 4 deletions spec/views/essences/essence_picture_editor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)
end

let(:options) { Hash.new }
let(:settings) { Hash.new }

before do
view.class.send(:include, Alchemy::Admin::BaseHelper)
Expand All @@ -32,8 +32,9 @@
end

subject do
allow(content).to receive(:settings) { settings }
render partial: "alchemy/essences/essence_picture_editor",
locals: {content: content, options: options}
locals: {content: content}
rendered
end

Expand All @@ -45,8 +46,10 @@
end

context "with settings[:deletable] being false" do
let(:options) do
{linkable: false}
let(:settings) do
{
linkable: false
}
end

it 'should not render a button to link and unlink the picture' do
Expand Down
35 changes: 35 additions & 0 deletions spec/views/essences/essence_select_editor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'alchemy/essences/_essence_select_editor' do
let(:content) { Alchemy::Content.new(essence: essence) }
let(:essence) { Alchemy::EssenceSelect.new }

before do
view.class.send(:include, Alchemy::Admin::BaseHelper)
allow(view).to receive(:content_label).and_return(content.name)
end

context 'if no select values are set' do
it 'renders a warning' do
render 'alchemy/essences/essence_select_editor', content: content
expect(rendered).to have_css('.warning')
end
end

context 'if select values are set' do
before do
allow(content).to receive(:settings) do
{
select_values: %w(red blue yellow)
}
end
end

it "renders a select box" do
render 'alchemy/essences/essence_select_editor', content: content
expect(rendered).to have_css('select.alchemy_selectbox')
end
end
end
Loading

0 comments on commit f0ed7a0

Please sign in to comment.