Sha256: 51d62b5d08849ad16d75904d8558d411dc453555a7bec81585d0cccc0c77b82f

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

require 'rails/generators/active_record'

module ScaffoldPlus
  module Generators
    class ForceSslGenerator < ActiveRecord::Generators::Base
      desc "Enforce SSL for this controller in production"
      argument :name, type: :string,
               desc: "The resource that is SSL protected"
      class_option :only, type: :array,
               desc: 'Enforce for these actions'
      class_option :except, type: :array,
               desc: 'Do not enforce for these actions'
      
      def update_controller
        file = "app/controllers/#{table_name}_controller.rb"
        inject_into_file file, after: /^class.*ApplicationController$/ do
          text = "\n  force_ssl if: :ssl_configured?"
          text << ", only: [ #{only_list} ]" if options.only.present?
          text << ", except: [ #{except_list} ]" if options.except.present?
          text
        end
        inject_into_file file, after: /private$/ do
          [
            "",
            "    # When to enforce SSL for this controller",
            "    def ssl_configured?",
            "      Rails.env.production?",
            "    end",
            "",
            ""
          ].join("\n")
        end
      end
      
      protected
      
      def only_list
        [ options.only ].join(", ")
      end
      
      def except_list
        [ options.except ].join(", ")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scaffold_plus-1.4.15 lib/generators/scaffold_plus/force_ssl/force_ssl_generator.rb