Module: PageRecord::Finders::ClassMethods
- Defined in:
- lib/page_record/finders.rb
Instance Method Summary collapse
-
#all(selector = nil, filter = nil) ⇒ Array Pagerecord
Searches the page and returns an Array of Base of instances of that match the selector and the filter.
-
#find(id = nil, selector = nil, filter = nil) ⇒ Pagerecord
Searches the page and returns an instance of Base of instances of that matches the given id, selector and the filter.
-
#find_by_attribute(attribute, value, selector, filter) ⇒ Pagerecord
Searches the page and returns an instance of Base of instances of that matches the given attribute.
Instance Method Details
#all(selector = nil, filter = nil) ⇒ Array Pagerecord
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/page_record/finders.rb', line 47 def all(selector = nil, filter = nil) selector ||= @selector filter ||= @filter records = [] context = context_for_selector(selector) context.all("[data-#{@type}-id]#{filter}").each do | record| id = record["data-#{@type}-id"] records << new(id, selector) end records end |
#find(id = nil, selector = nil, filter = nil) ⇒ Pagerecord
Searches the page and returns an instance of Base of instances of that matches the given id, selector and the filter. See markup for more details about formatting the page.
example:
TeamPage.find(1)
returns the record with id
When you don't specify an id, find
returns the only record on the page.
If you have more than one record on the page, find
raises MultipleRecords.
example:
TeamPage.find()
90 91 92 93 94 |
# File 'lib/page_record/finders.rb', line 90 def find(id = nil, selector = nil, filter = nil) selector ||= @selector filter ||= @filter new(id, selector, filter) end |
#find_by_attribute(attribute, value, selector, filter) ⇒ Pagerecord
Searches the page and returns an instance of Base of instances of that matches the given attribute. See markup for more details about formatting the page.
Although you can call this yourself, Base uses this method for defining a finder for all attributes when you define your page class, Base See Base.attributes for more details.
example:
TeamPage.find_by_name('Ajax')
returns the record where the name is set to Ajax
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/page_record/finders.rb', line 124 def find_by_attribute(attribute, value, selector, filter) selector ||= @selector filter ||= @filter context = context_for_selector(selector) record = context.find("[data-#{@type}-id]#{filter} > [data-attribute-for='#{attribute}']", text: value) parent = record.find(:xpath, '..') id = parent["data-#{@type}-id"] new(id, selector, filter) rescue Capybara::Ambiguous raise MultipleRecords, "Found multiple #{@type} record with #{attribute} #{value} on page" rescue Capybara::ElementNotFound raise RecordNotFound, "#{@type} record with #{attribute} #{value} not found on page" end |