Class: Webring::Generators::CustomWidgetControllerGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Shared::RouteInjector
Defined in:
lib/generators/webring/custom_widget_controller/custom_widget_controller_generator.rb

Overview

Note:

This generator should be run after installing the Webring engine

This generator creates both the controller file and adds the required routes

Examples:

The generated controller provides an endpoint for embedding the widget via the <script> tag:

# GET /widget.js - Get the widget's code for a script tag

Instance Method Summary collapse

Instance Method Details

#create_controller_fileObject

Creates the CustomWidgetController file based on the template



22
23
24
# File 'lib/generators/webring/custom_widget_controller/custom_widget_controller_generator.rb', line 22

def create_controller_file
  template 'custom_widget_controller.rb', 'app/controllers/webring/custom_widget_controller.rb'
end

#create_custom_widget_routesObject

Adds custom widget routes to the application’s routes.rb file These routes are used to create new custom widgets



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/generators/webring/custom_widget_controller/custom_widget_controller_generator.rb', line 28

def create_custom_widget_routes
  routes_file = 'config/routes.rb'
  widget_route = "get 'widget.js', to: 'widget#show', format: 'js', as: :widget"
  new_widget_route = "get 'widget.js', to: 'custom_widget#show', format: 'js', as: :widget"

  if File.read(routes_file).include?(widget_route)
    gsub_file routes_file, widget_route, new_widget_route
  else
    route_content = <<~ROUTE
      scope module: 'webring' do
        get 'widget.js', to: 'custom_widget#show', format: 'js', as: :widget
      end
    ROUTE

    inject_webring_routes(route_content)
  end
end