Module: PageRecord::Finders::ClassMethods

Defined in:
lib/page_record/finders.rb

Instance Method Summary collapse

Instance Method Details

#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. See markup for more details about formatting the page.

example:

TeamPage.all

returns all records on the page

Parameters:

  • selector (String) (defaults to: nil)

    selector to use for searching the table on the page

  • filter (String) (defaults to: nil)

    filter to use on the records on the page

Returns:

  • (Array Pagerecord)

    The Array containing instances of [PageRecord::Base] with records that fit the selector and the filter

Raises:

  • (MultipleRecords)

    if the page contains more then on set of records

  • (RecordNotFound)

    if the page does not contain any of the specified records



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()

Parameters:

  • selector (String) (defaults to: nil)

    selector to use for searching the table on the page

  • filter (String) (defaults to: nil)

    filter to use on the records on the page

Returns:

  • (Pagerecord)

    An instance of [PageRecord::Base]

Raises:

  • (MultipleRecords)

    if the page contains more then on set of records

  • (RecordNotFound)

    if the page does not contain any of the specified records



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

Parameters:

  • attribute (String)

    The attribute name

  • value (String)

    The value to search for

  • selector (String)

    selector to use for searching the table on the page

  • filter (String)

    filter to use on the records on the page

Returns:

  • (Pagerecord)

    An instance of [PageRecord::Base].

Raises:

  • (MultipleRecords)

    if the page contains more then on set of records

  • (RecordNotFound)

    if the page does not contain any of the specified records



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