Skip to content

Commit

Permalink
Change inheritance config from skipping to explicit inherit of sections
Browse files Browse the repository at this point in the history
  • Loading branch information
Envek committed Nov 24, 2017
1 parent ea1af70 commit d031ab9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions lib/tram/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 = {})
Expand Down
22 changes: 15 additions & 7 deletions lib/tram/page/section.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
# 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)
show?(page) ? { name => value(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
7 changes: 6 additions & 1 deletion spec/support/inherited_page.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/tram/inherited_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d031ab9

Please sign in to comment.