Sha256: 2796b1ba76bf23b8068df758ba9dfda8ae2d8567849b04099f6c0e0a71c6ce61

Contents?: true

Size: 1.84 KB

Versions: 24

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

require 'rails/generators'

# :nodoc:
module Spotlight
  # spotlight:scaffold_resource generator
  class ScaffoldResource < Rails::Generators::NamedBase
    source_root File.expand_path('templates', __dir__)

    def create_model
      create_file "app/models/#{file_name}_resource.rb", <<~FILE
        class #{class_name}Resource < Spotlight::Resource
          def self.indexing_pipeline
            @indexing_pipeline ||= super.dup.tap do |pipeline|
              # your pipeline here...
            end
          end
        end
      FILE
    end

    def create_controller
      create_file "app/controllers/#{file_name}_resources_controller.rb", <<~FILE
        class #{class_name}ResourcesController < Spotlight::ResourcesController
          private

          def resource_class
            #{class_name}Resource
          end
        end
      FILE
    end

    def create_form
      create_file "app/views/#{file_name}_resources/_form.html.erb", <<~FILE
        <%= bootstrap_form_for([current_exhibit, @resource.becomes(#{class_name}Resource)], as: :resource) do |f| %>
          <%= f.text_field :url  %>
          <div class="form-actions">
            <div class="primary-actions">
              <%= cancel_link @resource, :back, class: 'btn btn-secondary' %>
              <%= f.submit t('.add_item'), class: 'btn btn-primary' %>
            </div>
          </div>
        <% end if can? :manage, @resource %>
      FILE
    end
  end

  def inject_configuration
    inject_into_file 'config/initializers/spotlight_initializer.rb' do
      "\n  Spotlight::Engine.config.external_resources_partials += ['#{file_name}_resources/form']\n"
    end
  end

  def create_routes
    route <<~FILE
      resources :exhibits, only: [] do
        resources :#{file_name}_resources, only: [:create, :update] do
        end
      end
    FILE
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
blacklight-spotlight-4.3.6 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-4.3.5 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-4.3.4 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-4.3.3 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-4.3.2 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-4.3.1 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-4.3.0 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-4.2.0 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-4.1.2 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-4.1.1 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-4.1.0 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-4.0.3 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-4.0.2 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-4.0.1 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-4.0.0 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-3.6.0.beta10 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-3.6.0.beta9 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-3.6.0.beta8 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-3.6.0.beta7 lib/generators/spotlight/scaffold_resource_generator.rb
blacklight-spotlight-3.6.0.beta6 lib/generators/spotlight/scaffold_resource_generator.rb