Class: OasRails::ActiveRecordExampleFinder
- Inherits:
-
Object
- Object
- OasRails::ActiveRecordExampleFinder
- Defined in:
- lib/oas_rails/active_record_example_finder.rb
Instance Method Summary collapse
- #clean_example_object(obj:) ⇒ Object
-
#fetch_factory_bot_examples(klass:) ⇒ Hash
Fetches examples from FactoryBot for the provided class.
-
#fetch_fixture_examples(klass:) ⇒ Hash
Fetches examples from fixtures for the provided class.
-
#initialize(context: :incoming, utils: Utils, factory_bot: FactoryBot, erb: ERB, yaml: YAML, file: File) ⇒ ActiveRecordExampleFinder
constructor
A new instance of ActiveRecordExampleFinder.
- #search(klass) ⇒ Object
Constructor Details
#initialize(context: :incoming, utils: Utils, factory_bot: FactoryBot, erb: ERB, yaml: YAML, file: File) ⇒ ActiveRecordExampleFinder
Returns a new instance of ActiveRecordExampleFinder.
3 4 5 6 7 8 9 10 11 |
# File 'lib/oas_rails/active_record_example_finder.rb', line 3 def initialize(context: :incoming, utils: Utils, factory_bot: FactoryBot, erb: ERB, yaml: YAML, file: File) @context = context @utils = utils @factory_bot = factory_bot @erb = erb @yaml = yaml @file = file @factory_examples = {} end |
Instance Method Details
#clean_example_object(obj:) ⇒ Object
61 62 63 |
# File 'lib/oas_rails/active_record_example_finder.rb', line 61 def clean_example_object(obj:) obj.reject { |key, _| OasRails.config.send("excluded_columns_#{@context}").include?(key.to_sym) } end |
#fetch_factory_bot_examples(klass:) ⇒ Hash
Fetches examples from FactoryBot for the provided class.
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/oas_rails/active_record_example_finder.rb', line 28 def fetch_factory_bot_examples(klass:) klass_sym = @utils.class_to_symbol(klass) begin @factory_examples[klass_sym] = @factory_bot.build_stubbed_list(klass_sym, 1) if @factory_examples[klass_sym].nil? @factory_examples[klass_sym].each_with_index.to_h do |obj, index| ["#{klass_sym}#{index + 1}", { value: { klass_sym => clean_example_object(obj: obj.as_json) } }] end.deep_symbolize_keys rescue KeyError {} end end |
#fetch_fixture_examples(klass:) ⇒ Hash
Fetches examples from fixtures for the provided class.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/oas_rails/active_record_example_finder.rb', line 46 def fetch_fixture_examples(klass:) fixture_file = Rails.root.join('test', 'fixtures', "#{klass.to_s.pluralize.downcase}.yml") begin erb_result = @erb.new(@file.read(fixture_file)).result fixture_data = @yaml.safe_load( erb_result, aliases: true, permitted_classes: [Symbol, ActiveSupport::HashWithIndifferentAccess, Time] ).with_indifferent_access rescue Errno::ENOENT return {} end fixture_data.transform_values { |attributes| { value: { klass.to_s.downcase => clean_example_object(obj: attributes) } } }.deep_symbolize_keys end |
#search(klass) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/oas_rails/active_record_example_finder.rb', line 13 def search(klass) case @utils.detect_test_framework when :factory_bot fetch_factory_bot_examples(klass: klass) when :fixtures fetch_fixture_examples(klass: klass) else {} end end |