module UlePage::SitePrismExtender

Public Instance Methods

class_name(my_class) click to toggle source
# File lib/ule_page/site_prism_extender.rb, line 59
def class_name(my_class)
  my_class.name.underscore.downcase
end
define_elements(model_class, props = []) click to toggle source

can define fields based on the model class usage:

define_elements Brand
# File lib/ule_page/site_prism_extender.rb, line 25
def define_elements(model_class, props = [])
  attributes = model_class.new.attributes.keys

  props = attributes if props.empty?
  props.map!(&:to_s) unless props.empty?

  attributes.each do |attri|
    next unless props.include? attri

    selector = '#' + "#{class_name(model_class)}_#{attri}"

    element attri, selector
  end
end
define_elements_js(model_class, excluded_props = []) click to toggle source

instead of the rails traditional form in js mode, there is no prefix before the element name

# File lib/ule_page/site_prism_extender.rb, line 65
def define_elements_js(model_class, excluded_props = [])
  attributes = model_class.new.attributes.keys

  attributes.each do |attri|
    next if excluded_props.include? attri

    selector = '#' + attri
    # self.class.send "element", attri.to_sym, selector
    element attri, selector
  end
end
define_sub_elements(model_class, submodel_class, props = []) click to toggle source

if you want to define the element as model1[name] whose id is “model1_model2_name” useage

define Brand User
define Brand, User, [:description]
# File lib/ule_page/site_prism_extender.rb, line 44
def define_sub_elements(model_class, submodel_class, props = [])
  attributes = submodel_class.new.attributes.keys

  props = attributes if props.empty?
  props.map!(&:to_s) unless props.empty?

  attributes.each do |attri|
    next unless props.include?(attri)

    selector = '#' + "#{class_name(model_class)}_#{class_name(submodel_class)}_attributes_#{attri}"

    element attri, selector
  end
end
element_collection(collection_name, *find_args) click to toggle source

why define this method? if we use the elements method directly, it will be conflicted with RSpec.Mather.BuiltIn.All.elements. I have not found one good method to solve the confliction.

# File lib/ule_page/site_prism_extender.rb, line 11
def element_collection(collection_name, *find_args)
  build collection_name, *find_args do
    define_method collection_name.to_s do |*_runtime_args, &element_block|
      self.class.raise_if_block(self, collection_name.to_s, !element_block.nil?)
      page.all(*find_args)
    end
  end
end