Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow inline blocks to extend concrete classes #21

Closed

Conversation

khamusa
Copy link
Contributor

@khamusa khamusa commented Jan 10, 2025

Implements #19

With this change, users are able to reuse concrete classes as sections while also customizing their behavior inline, enabling advanced inheritance-like behaviors. Example:

class EmailAddressesSection < Tabasco::Section
  # ...
  def has_successful_notification_message?
    has_content!(success_notification)
  end
end

section :mailing_address, EmailAddressesSection do
  def success_notification
    "Mailing address updated successfully"
  end
end

section :living_address, EmailAddressesSection do
  def success_notification
    "Living address updated successfully"
  end
end

# ...

expect(form.mailing_address).to have_success_notification_message
expect(form.living_address).to have_success_notification_message

Implementation details

Given we don't want the original concrete class to be redefined, we subclass it before evaluating the inline block on its subclass. This was quite a fit given the DSL was already subclassing Section.

However, getting this to work properly required class instance variables that support the DSL configuration to be inherited by subclasses.

After experimenting with a variety of approaches, I decided to pull active support's class_attribute extension. It's a shame that this brings in a bunch of other dependencies, but for now this seemed like the best solution. We can revisit this later (maybe even copying over class_attribute's implementation given it's MIT-licensed).

I also had to figure out with the confusion around test_ids when using a concrete class (this was already a known logged issue). I decided the concrete class test id will never be used, unless when the class is being explicitly .loaded. This decision felt simply natural when using the framework. It feels consistent and foster code reuse.

@khamusa khamusa force-pushed the feature/19-inline-extension-of-concrete-classes branch 3 times, most recently from bd6a232 to 4850d43 Compare January 14, 2025 19:48
@khamusa khamusa mentioned this pull request Jan 14, 2025
lib/tabasco/section.rb Outdated Show resolved Hide resolved
@khamusa khamusa force-pushed the feature/19-inline-extension-of-concrete-classes branch from 1454123 to e3faf43 Compare January 22, 2025 19:19
@khamusa khamusa closed this Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants