Sha256: 7fb089524e065e1630602ab0732f56e4188cdfa976af0bbcc7845992ac591034
Contents?: true
Size: 1.62 KB
Versions: 2
Compression:
Stored size: 1.62 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 :helper, type: :boolean, default: false, banner: "create helper for view shortcuts" 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.helper? 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
bootswatch_rails-3.2.0.36 | lib/generators/bootswatch_rails/created_by/created_by_generator.rb |
bootswatch_rails-3.2.0.35 | lib/generators/bootswatch_rails/created_by/created_by_generator.rb |