Sha256: 923d7901756a175fa8a5d542fa5781b397ff37e630f67e5ccfa0617ab397deaf

Contents?: true

Size: 1.61 KB

Versions: 28

Compression:

Stored size: 1.61 KB

Contents

require 'rails/generators/active_record'

module BootswatchRails
  module Generators
    class CreatedByGenerator < ActiveRecord::Generators::Base
      desc "Add created_by and updated_by to resource"
      argument :name, type: :string,
               desc: "The resource to be updated"
      argument :user, type: :string, default: "user",
               banner: "name of the user model"
      class_option :init, type: :boolean, default: false,
               banner: "create helper for view helpers"
      class_option :migration, type: :boolean, default: false,
               desc: 'Create a migration for added attributes'
      source_root File.expand_path('../templates', __FILE__)
      
      def add_helper
        return unless options.init?
        template "created_by_helper.rb", "app/helpers/created_by_helper.rb"
      end
      
      def add_migration
        return unless options.migration?
        migration_template 'created_by_migration.rb', "db/migrate/#{migration_name}.rb"
      end
      
      def update_controller
        file = "app/controllers/#{table_name}_controller.rb"
        curr = "current_#{user}"
        inject_into_file file, after: /def update$/ do
          "\n    @#{name}.updated_by = #{curr} ? #{curr}.id : nil"
        end
        inject_into_file file, after: /@#{name} = #{class_name}\.new\(#{name}_params\)$/ do
          "\n    @#{name}.created_by = #{curr} ? #{curr}.id : nil" +
          "\n    @#{name}.updated_by = #{curr} ? #{curr}.id : nil"
        end
      end
      
      protected
      
      def migration_name
        "add_created_by_to_#{table_name}"
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
bootswatch_rails-3.3.7.0 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.6.2 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.6.1 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.5.2 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.5.1 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.4.1 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.2.1 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.1.2 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.1.1 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.1.0 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.0.11 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.0.10 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.0.9 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.0.8 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.0.7 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.0.6 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.0.5 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.0.4 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.0.3 lib/generators/bootswatch_rails/created_by/created_by_generator.rb
bootswatch_rails-3.3.0.2 lib/generators/bootswatch_rails/created_by/created_by_generator.rb