Sha256: f983c5e63be8fd869d05ec84334976e051380ec31d227051c01dd1dd985639a2

Contents?: true

Size: 1.46 KB

Versions: 46

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require "rails/generators/active_record/model/model_generator"

module Koi
  class ActiveRecordGenerator < ActiveRecord::Generators::ModelGenerator
    source_root ActiveRecord::Generators::ModelGenerator.source_root

    def admin_search
      "PgSearch::Model".safe_constantize ? pg_search : sql_search
    end

    def ordinal_scope
      return unless attributes.any? { |attr| attr.name == "ordinal" }

      insert_into_file "app/models/#{file_path}.rb", before: /end\Z/ do
        <<~RUBY
          default_scope -> { order(ordinal: :asc) }
        RUBY
      end
    end

    private

    def pg_search
      insert_into_file "app/models/#{file_path}.rb", after: "class #{class_name} < ApplicationRecord\n" do
        <<~RUBY
          include PgSearch::Model
        RUBY
      end

      insert_into_file "app/models/#{file_path}.rb", before: /end\Z/ do
        <<~RUBY
          pg_search_scope :admin_search, against: %i[#{search_fields.join(' ')}], using: { tsearch: { prefix: true } }
        RUBY
      end
    end

    def sql_search
      insert_into_file "app/models/#{file_path}.rb", before: /end\Z/ do
        clause = search_fields.map { |f| "#{f} LIKE :query" }.join(" OR ")
        <<~RUBY
          scope :admin_search, ->(query) do
            where("#{clause}", query: "%\#{query}%")
          end
        RUBY
      end
    end

    def search_fields
      attributes.select { |attr| attr.type == :string }.map(&:name)
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
katalyst-koi-4.15.1 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.15.0 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.14.3 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.14.2 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.14.1 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.14.0 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.13.2 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.13.0 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.12.6 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.12.5 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.12.4 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.12.3 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.12.2 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.12.1 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.12.0 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.11.2 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.11.1 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.11.0 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.10.3 lib/generators/koi/active_record/active_record_generator.rb
katalyst-koi-4.10.2 lib/generators/koi/active_record/active_record_generator.rb