<commit_before>require 'rspec/core'

module Saki
  module AcceptanceHelpers
    extend ActiveSupport::Concern

    def default_factory(name, opts = {})
      Factory name, opts
    end

    def get_path(path)
      if path.is_a? String
        path
      elsif path.is_a? Symbol
        send path
      else
        path.call(self)
      end
    end

    def add_opts(link, opts = {})
      if opts[:parent]
        link = "/#{opts[:parent].class.to_s.tableize}/#{opts[:parent].id}" + link
      end
      if opts[:format]
        link = link + ".#{opts[:format]}"
      end
      link
    end

    def has_link(href)
      page.should have_xpath("//a[@href='#{href}']")
    end

    def has_link_for(model, opts = {})
      href = add_opts "/#{model.class.to_s.tableize}/#{model.id}", opts
      page.should have_xpath("//a[@href='#{href}' and not(@rel)]")
    end

    def has_link_for_editing(model, opts = {})
      href = add_opts "/#{model.class.to_s.tableize}/#{model.id}/edit", opts
      has_link href
    end

    def has_link_for_deleting(model, opts = {})
      href = add_opts "/#{model.class.to_s.tableize}/#{model.id}", opts
      page.should have_xpath("//a[@href='#{href}' and @data-method='delete']")
    end

    def has_link_for_creating(model_type, opts = {})
      href = add_opts "/#{model_type.to_s.tableize}/new", opts
      has_link href
    end

    def has_link_for_indexing(model_type, opts = {})
      href = add_opts "/#{model_type.to_s.tableize}", opts
      has_link href
    end  

    def should_be_on(page_name)
      current_path = URI.parse(current_url).path
      if page_name.is_a? String
        current_path.should == page_name
      else
        current_path.should match(page_name)
      end
    end

    module ClassMethods
      def with_existing resource, opts={} &block
        context "with exisiting #{resource}" do
          before do
            instance_variable_set "@#{resource}", default_factory(resource, opts)
          end
          module_eval &block
        end
      end

      def on_following_link_to path, &block
        context "on following link" do
          before do
            path = get_path(path)
            has_link(path)<commit_msg>fixed with_existing bug<commit_after>require 'rspec/core'

module Saki
  module AcceptanceHelpers
    extend ActiveSupport::Concern

    def default_factory(name, opts = {})
      Factory name, opts
    end

    def get_path(path)
      if path.is_a? String
        path
      elsif path.is_a? Symbol
        send path
      else
        path.call(self)
      end
    end

    def add_opts(link, opts = {})
      if opts[:parent]
        link = "/#{opts[:parent].class.to_s.tableize}/#{opts[:parent].id}" + link
      end
      if opts[:format]
        link = link + ".#{opts[:format]}"
      end
      link
    end

    def has_link(href)
      page.should have_xpath("//a[@href='#{href}']")
    end

    def has_link_for(model, opts = {})
      href = add_opts "/#{model.class.to_s.tableize}/#{model.id}", opts
      page.should have_xpath("//a[@href='#{href}' and not(@rel)]")
    end

    def has_link_for_editing(model, opts = {})
      href = add_opts "/#{model.class.to_s.tableize}/#{model.id}/edit", opts
      has_link href
    end

    def has_link_for_deleting(model, opts = {})
      href = add_opts "/#{model.class.to_s.tableize}/#{model.id}", opts
      page.should have_xpath("//a[@href='#{href}' and @data-method='delete']")
    end

    def has_link_for_creating(model_type, opts = {})
      href = add_opts "/#{model_type.to_s.tableize}/new", opts
      has_link href
    end

    def has_link_for_indexing(model_type, opts = {})
      href = add_opts "/#{model_type.to_s.tableize}", opts
      has_link href
    end  

    def should_be_on(page_name)
      current_path = URI.parse(current_url).path
      if page_name.is_a? String
        current_path.should == page_name
      else
        current_path.should match(page_name)
      end
    end

    module ClassMethods
      def with_existing resource, opts={}, &block
        context "with exisiting #{resource}" do
          before do
            instance_variable_set "@#{resource}", default_factory(resource, opts)
          end
          module_eval &block
        end
      end

      def on_following_link_to path, &block
        context "on following link" do
          before do
            path = get_path(path)
            has_link(path)
{'fraction_non_alphanumeric': 0.1230526078121472, 'fraction_numerical': 0.0, 'mean_word_length': 2.3333333333333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3974141', 'n_tokens_mistral': 1439, 'n_tokens_neox': 1455, 'n_words': 315}