scaffold_plus 1.1.0 → 1.2.0

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: 59d4d6a91cc7d9676a1bbbc30337affed0c324da
4
- data.tar.gz: e7df5946f9fae05db957991eaa7ab4e6ff1f3651
3
+ metadata.gz: 5a8fb0c23d37ef6e25d422193e6be61c4715e125
4
+ data.tar.gz: e1fe09ea0e0277bec223e5780167524a4b636784
5
5
  SHA512:
6
- metadata.gz: 78184f050386f0ff6eb2f3c116303d904499336fef32de38839a14dbe23facfd09949c879d1d32446e087c9918e5fc6601501eb89d6489c91456cf9b659e0a80
7
- data.tar.gz: c1f5a56567b024cc4790f57fdb66e0f7f4c84338eabf1c13abc131c16cff2ae92f3b7a6696ae2db7714922bae1b10a01b07e16d7c4eb44ac8cdac314fcfa79a7
6
+ metadata.gz: 2551c7f4729513dc9e551fafa0562a597abe3a4a56f2d3ef61c7ac63214849be1ef60dba4a3747070c0a7698ad668393e5d63e95efdfd4bb61fd043253c8640b
7
+ data.tar.gz: 2e27fc1469ead9be8040d979f0d6908fca331b833d8a95ebf26596bd49e813bfa4fb11af6121de2a53c3a4d796066c88a411df30d605d92eb2778fefb76c260b
@@ -0,0 +1,63 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ScaffoldPlus
4
+ module Generators
5
+ class AncestryGenerator < ActiveRecord::Generators::Base
6
+ desc "Add ancestry to create a tree structure (or hierarchy)"
7
+ argument :name, type: :string,
8
+ desc: "The object that has_ancestry"
9
+ class_option :depth, type: :boolean, default: false,
10
+ desc: 'Cache the depth of each node'
11
+ class_option :migration, type: :boolean, default: false,
12
+ desc: 'Create a migration for added attributes'
13
+ class_option :index, type: :boolean, default: true,
14
+ desc: 'Add an index to the migration'
15
+ class_option :permit, type: :boolean, default: false,
16
+ desc: 'Allow mass assignment for added attributes'
17
+ class_option :before, type: :boolean, default: false,
18
+ desc: 'Add a line before generated text in model'
19
+ class_option :after, type: :boolean, default: false,
20
+ desc: 'Add a line after generated text in model'
21
+ source_root File.expand_path('../templates', __FILE__)
22
+
23
+ def add_migration
24
+ return unless options.migration?
25
+ migration_template "ancestry_migration.rb", "db/migrate/#{migration_name}.rb"
26
+ end
27
+
28
+ def add_to_model
29
+ inject_into_class "app/models/#{name}.rb", class_name do
30
+ text = options.before? ? "\n" : ""
31
+ text << " has_ancestry"
32
+ text << ", cache_depth: true" if options.depth?
33
+ text << "\n"
34
+ text << "\n" if options.after?
35
+ text
36
+ end
37
+ end
38
+
39
+ def add_to_permit
40
+ return unless options.permit?
41
+ text = ":ancestry"
42
+ file = "app/controllers/#{table_name}_controller.rb"
43
+ gsub_file file, /(permit\(.*)\)/, "\\1, #{text})"
44
+ # Special case: no previous permit
45
+ gsub_file file, /^(\s*params)\[:#{name}\]$/, "\\1.require(:#{name}).permit(#{text})"
46
+ end
47
+
48
+ protected
49
+
50
+ def migration_name
51
+ "add_ancestry_to_#{table_name}"
52
+ end
53
+
54
+ def create_index?
55
+ options.index?
56
+ end
57
+
58
+ def create_depth?
59
+ options.depth?
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,11 @@
1
+ class <%= migration_name.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ add_column :<%= table_name %>, :ancestry, :string
4
+ <%- if create_depth? -%>
5
+ add_column :<%= table_name %>, :ancestry_depth, :integer, default: 0
6
+ <%- end -%>
7
+ <%- if create_index? -%>
8
+ add_index :<%= table_name %>, :ancestry
9
+ <%- end -%>
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module ScaffoldPlus
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scaffold_plus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Volker Wiegand
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-15 00:00:00.000000000 Z
11
+ date: 2014-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -79,6 +79,8 @@ files:
79
79
  - Makefile
80
80
  - README.md
81
81
  - Rakefile
82
+ - lib/generators/scaffold_plus/ancestry/ancestry_generator.rb
83
+ - lib/generators/scaffold_plus/ancestry/templates/ancestry_migration.rb
82
84
  - lib/generators/scaffold_plus/collection/collection_generator.rb
83
85
  - lib/generators/scaffold_plus/collection/templates/view.html.erb
84
86
  - lib/generators/scaffold_plus/has_many/has_many_generator.rb