Skip to content

Content blocks: use settings and images#3968

Merged
mrcasals merged 27 commits intomasterfrom
content-blocks/use-options
Aug 30, 2018
Merged

Content blocks: use settings and images#3968
mrcasals merged 27 commits intomasterfrom
content-blocks/use-options

Conversation

@mrcasals
Copy link
Copy Markdown
Contributor

@mrcasals mrcasals commented Aug 6, 2018

🎩 What? Why?

This PR lets admins change the content blocks settings and images from the admin section.

📌 Related Issues

📋 Subtasks

  • Move sortable content blocks rows to cells
  • Add form to edit the content blocks settings
  • Add settings_form_cell to content blocks. This cell will receive a form object wrapping the content block, and should render the settings fields for the content block. This allows devs to add help texts and customize how to form looks.
  • Add images
  • Move "hero" content block data to settings and add migration
  • Add/modify seeds
  • Add system specs
  • Add specs for UpdateContentBlock command
  • Add documentation regarding the feature

@mrcasals mrcasals self-assigned this Aug 6, 2018
@ghost ghost added the status: WIP label Aug 6, 2018
@mrcasals mrcasals force-pushed the content-blocks/use-options branch 3 times, most recently from f1e497f to f5e3cf7 Compare August 6, 2018 10:24
@mrcasals mrcasals force-pushed the content-blocks/use-options branch from c79c3d8 to b3029d6 Compare August 6, 2018 14:16
# Public: Initializes the command.
#
# form - The form from which the data in this component comes from.
# component - The component to update.

This comment was marked as resolved.

This comment was marked as resolved.


module Decidim
module Admin
# This command gets called when a contnt block is updated from the admin panel.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/contnt/content

@scope = scope
end

# Public: Creates the Component.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong docs

def call
return broadcast(:invalid) if form.invalid?

update_content_block_settings
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap these in a transaction?

def update_content_block_images
content_block.image_names.each do |image_name|
attachment = content_block.images.send(image_name)
byebug
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this

elsif form.images[image_name]
update_image(attachment, image_name, form.images[image_name])
end

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra line

@content_blocks ||= Decidim::ContentBlock.for_scope(:homepage, organization: current_organization)
end

def used_manifests
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename used_manifests to current_manifests and unused_manifests to available_manifests?

Although it's a bit weird to talk about manifests when we seem to want content blocks

def define_methods
block.manifest.image_names.each do |image_name|
self.class.define_method image_name do
Attachment.find_by(attached_to:@block, title: { name: image_name })
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be memoized?

# Public: Registers the cell this content block will use to render the
# settings form in the admin section. Use `#settings_form_cell_name` to
# retrieve it.
def settings_form_cell(cell_name)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be settings_form_cell=?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, to unify the API. This is the same strategy I followed for cell/cell_name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I think of it, I can change the API to use = so that I can remove all these weird-looking and confusing methods.

@mrcasals mrcasals changed the title [WIP] Content blocks: use settings [WIP] Content blocks: use settings and images Aug 7, 2018
@mrcasals mrcasals force-pushed the content-blocks/use-options branch from 2634979 to b8dff39 Compare August 7, 2018 13:10
@mrcasals mrcasals force-pushed the content-blocks/use-options branch 2 times, most recently from 6c25f78 to edd9b32 Compare August 9, 2018 10:47
@mrcasals mrcasals force-pushed the content-blocks/use-options branch from edd9b32 to ec779c2 Compare August 9, 2018 11:08
@mrcasals mrcasals force-pushed the content-blocks/use-options branch from ab08010 to f1c0f0c Compare August 10, 2018 10:17
@mrcasals mrcasals changed the title [WIP] Content blocks: use settings and images Content blocks: use settings and images Aug 10, 2018
end
end

def content_block_from_manifest
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a bit weird? Creating a content block at the controller in this way its' kind of unexpected 🤨

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to modify the DB here, any suggestion on how to improve this?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, I don't know how to solve it.

validates :default_locale, inclusion: { in: :available_locales }

mount_uploader :homepage_image, Decidim::HomepageImageUploader
mount_uploader :homepage_image, Decidim::HomepageImageUploader # DEPRECATED
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this removed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I need to access this uploader from the migration. Maybe I can remove this from here and reopen the class in the migration, add the mount_uploader part there and keep this clean?

url: request.original_url,
twitter_handler: current_organization.twitter_handler,
image_url: current_organization.homepage_image.url
image_url: Decidim::ContentBlock.published.find_by(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add this logic to the organization, so the external api doesn't change.

settings = welcome_text.inject(settings) { |acc, (k, v)| acc.update("welcome_text_#{k}" => v) }

content_block.settings = settings
content_block.images_container.background_image = organization.homepage_image.file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried this with "real" data?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it with the development app, which had this data. Does this count as "real data"?

def image_names_are_unique
errors.add(:image_names, :invalid) if image_names.count != image_names.uniq.count
image_names = images.map { |image| image[:name] }
errors.add(:images, "names must be unique per manifest") if image_names.count != image_names.uniq.count
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I18n?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed, this will be executed on application startup, the error is not for end users.

class ImageNameCannotBeBlank < StandardError; end
def images_have_an_uploader
uploaders = images.map { |image| image[:uploader].presence }
errors.add(:images, "must have an uploader") if uploaders.compact.count != images.count
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I18n?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

sequence(:host) { |n| "#{n}.lvh.me" }
description { Decidim::Faker::Localized.wrapped("<p>", "</p>") { Decidim::Faker::Localized.sentence(2) } }
welcome_text { Decidim::Faker::Localized.wrapped("<p>", "</p>") { Decidim::Faker::Localized.sentence(2) } }
homepage_image { Decidim::Dev.test_file("city.jpeg", "image/jpeg") }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we create content blocks at the factory? Or maybe we shouldn't?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, but I don't really think it's needed in most cases

rails "db:migrate"
end

rails "db:migrate"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? If you had to change this maybe you forgot to define a class inside a migration.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the migration I need to access elements defined in the ContentBlock model (I need to access the images_container dynamic class). If I redefine the class in the migration then I cannot access that dynamic class, and makes life a lot harder.

I've talked to @deivid-rodriguez and agreed to change this file.

@ghost ghost added the status: WIP label Aug 10, 2018
@mrcasals mrcasals mentioned this pull request Aug 30, 2018
18 tasks
@mrcasals mrcasals merged commit e06bef7 into master Aug 30, 2018
@mrcasals mrcasals deleted the content-blocks/use-options branch August 30, 2018 10:09
@oriolgual oriolgual mentioned this pull request Nov 22, 2018
2 tasks
@mrcasals mrcasals added this to the CDP4 milestone Dec 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants