Skip to content

Commit

Permalink
Merge pull request #1638 from tvdeyen/no-local-options-in-editors
Browse files Browse the repository at this point in the history
Remove local options from essence editors
  • Loading branch information
tvdeyen authored Oct 21, 2019
2 parents 5af8727 + f0ed7a0 commit d46e769
Show file tree
Hide file tree
Showing 44 changed files with 305 additions and 256 deletions.
15 changes: 1 addition & 14 deletions app/controllers/alchemy/admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class BaseController < Alchemy::BaseController
before_action { enforce_ssl if ssl_required? && !request.ssl? }
before_action :load_locked_pages

helper_method :clipboard_empty?, :trash_empty?, :get_clipboard, :is_admin?,
:options_from_params
helper_method :clipboard_empty?, :trash_empty?, :get_clipboard, :is_admin?

check_authorization

Expand Down Expand Up @@ -128,18 +127,6 @@ def do_redirect_to(url_or_path)
end
end

# Extracts options from params and permits all keys
#
# If no options are present it returns an empty parameters hash.
#
# @returns [ActionController::Parameters]
def options_from_params
@_options_from_params ||= begin
(params[:options] || ActionController::Parameters.new).permit!
end
end
deprecate :options_from_params, deprecator: Alchemy::Deprecation

# This method decides if we want to raise an exception or not.
#
# I.e. in test environment.
Expand Down
1 change: 0 additions & 1 deletion app/controllers/alchemy/admin/contents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class ContentsController < Alchemy::Admin::BaseController

def create
@content = Content.create(content_params)
@html_options = params[:html_options] || {}
end

private
Expand Down
18 changes: 8 additions & 10 deletions app/controllers/alchemy/admin/essence_pictures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ def edit
def crop
if @picture = @essence_picture.picture
@content = @essence_picture.content
options_from_params[:format] ||= (configuration(:image_store_format) || 'png')

@min_size = sizes_from_essence_or_params
@ratio = ratio_from_size_or_params
@ratio = ratio_from_size_or_settings
infer_width_or_height_from_ratio

@default_box = @essence_picture.default_mask(@min_size)
Expand Down Expand Up @@ -70,24 +68,24 @@ def load_content

# Gets the minimum size of the image to be rendered.
#
# The +render_size+ attribute has preference over the +size+ parameter.
# The +render_size+ attribute has preference over the contents +size+ setting.
#
def sizes_from_essence_or_params
if @essence_picture.render_size?
@essence_picture.sizes_from_string(@essence_picture.render_size)
elsif options_from_params[:size]
@essence_picture.sizes_from_string(options_from_params[:size])
elsif @essence_picture.content.settings[:size]
@essence_picture.sizes_from_string(@essence_picture.content.settings[:size])
else
{ width: 0, height: 0 }
end
end

# Infers the aspect ratio from size or parameters. If you don't want a fixed
# Infers the aspect ratio from size or contents settings. If you don't want a fixed
# aspect ratio, don't specify a size or only width or height.
#
def ratio_from_size_or_params
if @min_size.value?(0) && options_from_params[:fixed_ratio].to_s =~ FLOAT_REGEX
options_from_params[:fixed_ratio].to_f
def ratio_from_size_or_settings
if @min_size.value?(0) && @essence_picture.content.settings[:fixed_ratio].to_s =~ FLOAT_REGEX
@essence_picture.content.settings[:fixed_ratio].to_f
elsif !@min_size[:width].zero? && !@min_size[:height].zero?
@min_size[:width].to_f / @min_size[:height].to_f
else
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/alchemy/admin/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ def search_filter_params

def common_search_filter_includes
[
# contrary to Rails' documentation passing an empty hash to permit all keys does not work
{options: options_from_params.keys},
{q: [
resource_handler.search_field_name,
:s
Expand Down
44 changes: 15 additions & 29 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,33 +54,33 @@ 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

# Renders a thumbnail for given EssencePicture content with correct cropping and size
def essence_picture_thumbnail(content, options = {})
def essence_picture_thumbnail(content)
picture = content.ingredient
essence = content.essence

return if picture.nil?

image_tag(
essence.thumbnail_url(options),
essence.thumbnail_url,
alt: picture.name,
class: 'img_paddingtop',
title: Alchemy.t(:image_name) + ": #{picture.name}"
)
end

# Size value for edit picture dialog
def edit_picture_dialog_size(content, options = {})
if content.settings_value(:caption_as_textarea, options)
content.settings_value(:sizes, options) ? '380x320' : '380x300'
def edit_picture_dialog_size(content)
if content.settings[:caption_as_textarea]
content.settings[:sizes] ? '380x320' : '380x300'
else
content.settings_value(:sizes, options) ? '380x290' : '380x255'
content.settings[:sizes] ? '380x290' : '380x255'
end
end

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/models/alchemy/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def tinymce_class_name
#
# If the value is a symbol it gets passed through i18n
# inside the +alchemy.default_content_texts+ scope
def default_text(default)
def default_value(default = definition[:default])
case default
when Symbol
Alchemy.t(default, scope: :default_content_texts)
Expand Down
31 changes: 17 additions & 14 deletions app/models/alchemy/content/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ def new(attributes = {})
if definition.blank?
raise ContentDefinitionError, "No definition found in elements.yml for #{attributes.inspect} and #{element.inspect}"
end
super(name: definition['name'], element_id: element.id)
super(
name: definition[:name],
essence_type: normalize_essence_type(definition[:type]),
element_id: element.id
).tap(&:build_essence)
end
alias_method :build, :new
deprecate build: :new, deprecator: Alchemy::Deprecation
Expand All @@ -43,7 +47,7 @@ def create(*args)
element = attributes[:element]
end
new(attributes.merge(element: element)).tap do |content|
content.create_essence!(attributes[:essence_type])
content.essence.save && content.save
end
end
alias_method :create_from_scratch, :create
Expand Down Expand Up @@ -119,12 +123,22 @@ def definition
element.content_definition_for(name) || {}
end

# Build essence from definition.
#
# If an optional type is passed, this type of essence gets created.
#
def build_essence(type = essence_type)
self.essence = essence_class(type).new({
ingredient: default_value
})
end

# Creates essence from definition.
#
# If an optional type is passed, this type of essence gets created.
#
def create_essence!(type = nil)
self.essence = essence_class(type).create!(prepared_attributes_for_essence)
build_essence(type).save!
save!
end

Expand All @@ -137,16 +151,5 @@ def create_essence!(type = nil)
def essence_class(type = nil)
Content.normalize_essence_type(type || definition['type']).constantize
end

# Prepares the attributes for creating the essence.
#
# 1. It sets a default text if given in +elements.yml+
#
def prepared_attributes_for_essence
attributes = {
ingredient: default_text(definition['default'])
}
attributes
end
end
end
16 changes: 8 additions & 8 deletions app/models/alchemy/essence_picture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ def picture_url_options
# image displayed in the frontend.
#
# @return [String]
def thumbnail_url(options = {})
def thumbnail_url
return if picture.nil?

crop = crop_values_present? || content.settings_value(:crop, options)
size = render_size || content.settings_value(:size, options)
crop = crop_values_present? || content.settings[:crop]
size = render_size || content.settings[:size]

options = {
size: thumbnail_size(size, crop),
Expand Down Expand Up @@ -132,12 +132,12 @@ def serialized_ingredient
picture_url(content.settings)
end

# Show image cropping link for content and options?
def allow_image_cropping?(options = {})
content && content.settings_value(:crop, options) && picture &&
# Show image cropping link for content
def allow_image_cropping?
content && content.settings[:crop] && picture &&
picture.can_be_cropped_to(
content.settings_value(:size, options),
content.settings_value(:upsample, options)
content.settings[:size],
content.settings[:upsample]
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
in_dialog: true,
redirect_url: admin_attachments_path(
element_id: @element.try(:id),
content_id: @content.try(:id),
options: options_from_params
content_id: @content.try(:id)
) %>
<% end %>
<%= render 'alchemy/admin/partials/remote_search_form' %>
Expand Down
3 changes: 1 addition & 2 deletions app/views/alchemy/admin/attachments/_file_to_assign.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<li class="assign_file_file <%= cycle('even', 'odd') %>">
<%= link_to alchemy.assign_admin_essence_files_path(
attachment_id: file_to_assign.id,
content_id: @content.id,
options: options_from_params
content_id: @content.id
),
remote: true,
method: 'put' do %>
Expand Down
4 changes: 1 addition & 3 deletions app/views/alchemy/admin/contents/_missing.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
content: {
element_id: element.id,
name: name
},
was_missing: true,
options: options
}
),
'data-create-missing-content' => true,
class: 'small button' %>
Expand Down
4 changes: 1 addition & 3 deletions app/views/alchemy/admin/contents/create.js.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
var editor_html = '<%= j(render "alchemy/essences/#{@content.essence_partial_name}_editor", {
content: @content, options: options_from_params, html_options: @html_options
}) %>';
var editor_html = '<%= j render("alchemy/essences/#{@content.essence_partial_name}_editor", content: @content) %>';

$("[data-element-<%= @content.element_id %>-missing-content=\"<%= @content.name %>\"]").replaceWith(editor_html);

Expand Down
3 changes: 1 addition & 2 deletions app/views/alchemy/admin/essence_files/assign.js.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
$('#<%= @content.dom_id %>').replaceWith('<%= j(
render "alchemy/essences/essence_file_editor",
formats: [:html],
content: @content,
options: options_from_params
content: @content
) %>');
Alchemy.closeCurrentDialog();
Alchemy.setElementDirty($('#element_<%= @content.element.id %>'));
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
3 changes: 1 addition & 2 deletions app/views/alchemy/admin/essence_pictures/assign.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ $('#picture_to_assign_<%= @picture.id %> a').attr('href', '#').off('click');
$('#<%= @content.dom_id -%>').replaceWith('<%= j(
render(
"alchemy/essences/essence_picture_editor",
content: @content,
options: options_from_params
content: @content
)
) %>');
Alchemy.closeCurrentDialog();
Expand Down
2 changes: 1 addition & 1 deletion app/views/alchemy/admin/essence_pictures/crop.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</div>
<%= form_for(
@essence_picture,
url: alchemy.admin_essence_picture_path(@essence_picture, options: options_from_params),
url: alchemy.admin_essence_picture_path(@essence_picture),
id: 'image_cropper_form',
remote: true
) do |f| %>
Expand Down
14 changes: 7 additions & 7 deletions app/views/alchemy/admin/essence_pictures/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<%= alchemy_form_for @essence_picture,
url: alchemy.admin_essence_picture_path(@essence_picture, options: options_from_params) do |f| %>
url: alchemy.admin_essence_picture_path(@essence_picture) do |f| %>
<%= hidden_field_tag 'content_id', @content.id %>
<%= f.input :caption, as: options_from_params[:caption_as_textarea] ? 'text' : 'string' %>
<%= f.input :caption, as: @content.settings[:caption_as_textarea] ? 'text' : 'string' %>
<%= f.input :title %>
<%= f.input :alt_tag %>
<%- if options_from_params[:sizes].present? -%>
<%- if @content.settings[:sizes].present? -%>
<%= f.input :render_size,
collection: [
[Alchemy.t('Layout default'), options_from_params[:size]]
] + options_from_params[:sizes].to_a,
[Alchemy.t('Layout default'), @content.settings[:size]]
] + @content.settings[:sizes].to_a,
include_blank: false,
input_html: {class: 'alchemy_selectbox'} %>
<%- end -%>
<%- if options_from_params[:css_classes].present? -%>
<%- if @content.settings[:css_classes].present? -%>
<%= f.input :css_class,
collection: options_from_params[:css_classes],
collection: @content.settings[:css_classes],
include_blank: Alchemy.t('None'),
input_html: {class: 'alchemy_selectbox'} %>
<%- else -%>
Expand Down
Loading

0 comments on commit d46e769

Please sign in to comment.