diff options
Diffstat (limited to 'qa/qa/factory/product.rb')
-rw-r--r-- | qa/qa/factory/product.rb | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/qa/qa/factory/product.rb b/qa/qa/factory/product.rb index 996b7f14f61..17fe908eaa2 100644 --- a/qa/qa/factory/product.rb +++ b/qa/qa/factory/product.rb @@ -5,26 +5,46 @@ module QA class Product include Capybara::DSL + NoValueError = Class.new(RuntimeError) + + attr_reader :factory, :web_url + Attribute = Struct.new(:name, :block) - def initialize - @location = current_url + def initialize(factory, web_url) + @factory = factory + @web_url = web_url + + populate_attributes! end def visit! - visit @location + visit(web_url) + end + + def self.populate!(factory, web_url) + new(factory, web_url) end - def self.populate!(factory) - new.tap do |product| - factory.class.attributes.each_value do |attribute| - product.instance_exec(factory, attribute.block) do |factory, block| - value = block.call(factory) - product.define_singleton_method(attribute.name) { value } - end + private + + def populate_attributes! + factory.class.attributes.each do |attribute| + instance_exec(factory, attribute.block) do |factory, block| + value = attribute_value(attribute, block) + + raise NoValueError, "No value was computed for product #{attribute.name} of factory #{factory.class.name}." unless value + + define_singleton_method(attribute.name) { value } end end end + + def attribute_value(attribute, block) + factory.api_resource&.dig(attribute.name) || + (block && block.call(factory)) || + (factory.respond_to?(attribute.name) && factory.public_send(attribute.name)) + end end end end |