scaffold_plus 1.4.2 → 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dbdd8a87ff690f0bfa137df59728909141186200
4
- data.tar.gz: 03705405a7d01856315b3d8370fa884c389ae672
3
+ metadata.gz: 8fbd259fcac0298c908522ca9cc07e94e86d343c
4
+ data.tar.gz: b57179a63c9d9995d93eefd98c53690f020efceb
5
5
  SHA512:
6
- metadata.gz: 316f2369bd8531ab12cc0f1d733e3411674e1b05fa4b63cd53caf507f9a9cf543610b9dc75dddb00bb2bb52bb61ed8df9fee37c1226f644b39670b2f9afbee8e
7
- data.tar.gz: 6998c1f2bdc6cf87789fa0b930ef8636a10f749b86acc0cd7df50bb8339e5e66bd9ba701c6fcb1ad5372e426c8ee1857512e963857413c0323f6b411e703dc5b
6
+ metadata.gz: 2b94932cb072134121cca35beaaf469f5faa2d336a84ece04c0ae4da2d1fcfd17684dfd96cd1aa88f9d63b9d845fb8783a9cfab90a641919cf720d03850f89ca
7
+ data.tar.gz: 44af175f14fcdb1d051beb72e1b44c5a1e4d9b94976600e7bc68745a71d3d1b76fd002f75811bdbfd7cbe16a84e030eaf50680b6c4be4087129164f8db8e2f1d
@@ -0,0 +1,56 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ScaffoldPlus
4
+ module Generators
5
+ class CreatedByGenerator < ActiveRecord::Generators::Base
6
+ desc "Add created_by and updated_by to resource"
7
+ argument :name, type: :string,
8
+ desc: "The resource to be updated"
9
+ argument :user, type: :string, default: "user",
10
+ banner: "user model (default 'user')"
11
+ class_option :migration, type: :boolean, default: true,
12
+ desc: 'Create a migration for added attributes'
13
+ class_option :before, type: :boolean, default: false,
14
+ desc: 'Add a line before generated text in model'
15
+ class_option :after, type: :boolean, default: false,
16
+ desc: 'Add a line after generated text in model'
17
+ source_root File.expand_path('../templates', __FILE__)
18
+
19
+ def add_migration
20
+ return unless options.migration?
21
+ migration_template 'created_by_migration.rb', "db/migrate/#{migration_name}.rb"
22
+ end
23
+
24
+ def add_to_model
25
+ [ "created", "updated" ].each do |action|
26
+ lines = options.before? ? [ "" ] : []
27
+ lines << [
28
+ " def #{action}_by_name",
29
+ " #{user.camelize}.find(#{action}_by).try('name')",
30
+ " end"
31
+ ""
32
+ ]
33
+ lines << [ "" ] if options.after?
34
+ inject_into_class "app/models/#{name}.rb", class_name, lines.join("\n")
35
+ end
36
+ end
37
+
38
+ def add_to_controller
39
+ file = "app/controllers/#{table_name}_controller.rb"
40
+ inject_into_file file, after: /@#{name} = #{class_name}\.new\(#{name}_params\)$/ do
41
+ " @#{name}.created_by = current_#{user}.id if current_#{user}\n" +
42
+ " @#{name}.updated_by = current_#{user}.id if current_#{user}\n"
43
+ end
44
+ inject_into_file file, after: /def update$/ do
45
+ " @#{name}.updated_by = current_#{user}.id if current_#{user}\n"
46
+ end
47
+ end
48
+
49
+ protected
50
+
51
+ def migration_name
52
+ "add_created_by_to_#{name}"
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,6 @@
1
+ class <%= migration_name.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ add_column :<%= table_name %>, :created_by, :integer
4
+ add_column :<%= table_name %>, :updated_by, :integer
5
+ end
6
+ end
@@ -1,3 +1,3 @@
1
1
  module ScaffoldPlus
2
- VERSION = "1.4.2"
2
+ VERSION = "1.4.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scaffold_plus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Volker Wiegand
@@ -84,6 +84,8 @@ files:
84
84
  - lib/generators/scaffold_plus/autofocus/autofocus_generator.rb
85
85
  - lib/generators/scaffold_plus/collection/collection_generator.rb
86
86
  - lib/generators/scaffold_plus/collection/templates/view.html.erb
87
+ - lib/generators/scaffold_plus/created_by/created_by_generator.rb
88
+ - lib/generators/scaffold_plus/created_by/templates/created_by_migration.rb
87
89
  - lib/generators/scaffold_plus/habtm/habtm_generator.rb
88
90
  - lib/generators/scaffold_plus/habtm/templates/habtm_migration.rb
89
91
  - lib/generators/scaffold_plus/has_many/has_many_generator.rb