Content blocks: use settings and images#3968
Conversation
f1e497f to
f5e3cf7
Compare
c79c3d8 to
b3029d6
Compare
| # 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.
Sorry, something went wrong.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
|
|
||
| module Decidim | ||
| module Admin | ||
| # This command gets called when a contnt block is updated from the admin panel. |
| @scope = scope | ||
| end | ||
|
|
||
| # Public: Creates the Component. |
| def call | ||
| return broadcast(:invalid) if form.invalid? | ||
|
|
||
| update_content_block_settings |
There was a problem hiding this comment.
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 |
| elsif form.images[image_name] | ||
| update_image(attachment, image_name, form.images[image_name]) | ||
| end | ||
|
|
| @content_blocks ||= Decidim::ContentBlock.for_scope(:homepage, organization: current_organization) | ||
| end | ||
|
|
||
| def used_manifests |
There was a problem hiding this comment.
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 }) |
| # 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) |
There was a problem hiding this comment.
Shouldn't this be settings_form_cell=?
There was a problem hiding this comment.
No, to unify the API. This is the same strategy I followed for cell/cell_name
There was a problem hiding this comment.
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.
2634979 to
b8dff39
Compare
Options are hard-coded by now
6c25f78 to
edd9b32
Compare
edd9b32 to
ec779c2
Compare
ab08010 to
f1c0f0c
Compare
| end | ||
| end | ||
|
|
||
| def content_block_from_manifest |
There was a problem hiding this comment.
Isn't this a bit weird? Creating a content block at the controller in this way its' kind of unexpected 🤨
There was a problem hiding this comment.
I need to modify the DB here, any suggestion on how to improve this?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Have you tried this with "real" data?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
| 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") } |
There was a problem hiding this comment.
Can't we create content blocks at the factory? Or maybe we shouldn't?
There was a problem hiding this comment.
We can, but I don't really think it's needed in most cases
| rails "db:migrate" | ||
| end | ||
|
|
||
| rails "db:migrate" |
There was a problem hiding this comment.
Why? If you had to change this maybe you forgot to define a class inside a migration.
There was a problem hiding this comment.
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.
🎩 What? Why?
This PR lets admins change the content blocks settings and images from the admin section.
📌 Related Issues
📋 Subtasks
settings_form_cellto content blocks. This cell will receive aformobject 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.UpdateContentBlockcommand