diff --git a/README.md b/README.md index f507022..7fcd55a 100644 --- a/README.md +++ b/README.md @@ -56,15 +56,16 @@ Inheritance of page objects is supported: ```ruby class FancyIndexPage < IndexPage - section :collection, skip: true - section :fancy_collection + inherit_section :title + section :fancy_collection + inherit_section :index_url, if: :readonly_on? def fancy_collection collection.map(&:fancy) end end -FancyIndexPage.new(Account.find(99)).to_h +FancyIndexPage.new(Account.find(99)).to_h # => { :title => "…", fancy_collection: […] } ``` ## Development diff --git a/lib/tram/page.rb b/lib/tram/page.rb index aa8aac8..3962f5b 100644 --- a/lib/tram/page.rb +++ b/lib/tram/page.rb @@ -20,6 +20,17 @@ def section(name, options = {}) @__sections[n] = section end + def sections + @__sections + end + + def inherit_section(name, option_overrides={}) + n = name.to_sym + parent_section = superclass.sections[n] + options = Tram::Page::Section.dry_initializer.attributes(parent_section) + section(name, options.merge(option_overrides)) + end + def url_helper(name) raise "Rails url_helpers module is not defined" unless defined?(Rails) delegate name, to: :"Rails.application.routes.url_helpers" @@ -30,10 +41,6 @@ def url_helper(name) def define_section_method(n, section) define_method(n) { section.value(self) } end - - def inherited(subclass) - subclass.instance_variable_set(:@__sections, @__sections.dup) - end end def to_h(options = {}) diff --git a/lib/tram/page/section.rb b/lib/tram/page/section.rb index 0212408..8b9fb51 100644 --- a/lib/tram/page/section.rb +++ b/lib/tram/page/section.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true class Tram::Page::Section - extend Dry::Initializer + extend ::Dry::Initializer param :name - option :value, optional: true, as: :getter - option :if, optional: true, as: :positive - option :unless, optional: true, as: :negative + option :value, optional: true + option :if, optional: true + option :unless, optional: true # Takes the page instance and extracts a hash for the section def call(page) @@ -14,15 +14,23 @@ def call(page) end def value(page) - getter ? page.instance_exec(&getter) : page.public_send(name) + if attributes[:value] + page.instance_exec(&attributes[:value]) + else + page.public_send(name) + end end private + def attributes + @attributes ||= self.class.dry_initializer.attributes(self) + end + def show?(page) condition = true - condition &= page.send(positive) if positive - condition &= !page.send(negative) if negative + condition &= page.__send__(attributes[:if]) if attributes[:if] + condition &= !page.__send__(attributes[:unless]) if attributes[:unless] condition end end diff --git a/spec/support/inherited_page.rb b/spec/support/inherited_page.rb index 63c6e92..9f9b1dc 100644 --- a/spec/support/inherited_page.rb +++ b/spec/support/inherited_page.rb @@ -1,10 +1,15 @@ # frozen_string_literal: true class InheritedPage < ExamplePage - section :baz, skip: true + inherit_section :foo + inherit_section :bar, if: :no_bar section :bam def bam baz.upcase end + + def no_bar + !bar + end end diff --git a/spec/tram/inherited_page_spec.rb b/spec/tram/inherited_page_spec.rb index 9e05c17..db1eb32 100644 --- a/spec/tram/inherited_page_spec.rb +++ b/spec/tram/inherited_page_spec.rb @@ -6,7 +6,7 @@ subject { described_class.new("test") } it "returns data hash with new and without skipped fields" do - expect(subject.to_h).to eq(bar: "test", foo: "test", bam: "TEST") + expect(subject.to_h).to eq(foo: "test", bam: "TEST") end it "doesn't change behaviour of the parent class" do