scaffold_plus 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ccb0a11c7666b4b3c87b0bdcfb5afa00c76c2ddc
4
+ data.tar.gz: 972a3aab0ad4f8089081feb9f4e93ecf8b898fcc
5
+ SHA512:
6
+ metadata.gz: c40c82a28076a7fc67f1018898fcee06434e85fe3ca7b401e07131b2f0e2699b2c168f770e6bb3b60fe7ce2ae34a6a0c60d3afb97310dfd710303f83a88e4cd3
7
+ data.tar.gz: a30d55d4f66ee3206d5632d0a8ebc523f6ac805bfbb6345cd8460b6c38879366517090a75a537b5247d9ff3f91c93cd454b6f5dbb7f3bf909aed4f10c0379cdd
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in scaffold_plus.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Volker Wiegand
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Makefile ADDED
@@ -0,0 +1,11 @@
1
+ # vim: set ts=8 tw=0 noet :
2
+ #
3
+ # Makefile for building the Gem
4
+ #
5
+
6
+ all:
7
+ git add lib
8
+ vim lib/scaffold_plus/version.rb
9
+ git commit -a
10
+ rake release
11
+
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # ScaffoldPlus
2
+
3
+ A collection of little helpers for Rails scaffolding
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'scaffold_plus'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install scaffold_plus
18
+
19
+ ## Usage
20
+
21
+ ### Add regular one-to-many association (has_many / belongs_to)
22
+ rails generate scaffold_plus:has_many
23
+
24
+ This helper adds parent#has_many and child#belongs_to to the models
25
+ and updates the mass assignment whitelist in the controller.
26
+ It can also add a migration for the parent_id and a counter.
27
+
28
+ ## Testing
29
+
30
+ Since I have no experience with test driven development (yet), this is
31
+ still an empty spot. Any help is highly appreciated.
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/volkerwiegand/scaffold_plus/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,104 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ScaffoldPlus
4
+ module Generators
5
+ class HasManyGenerator < ActiveRecord::Generators::Base
6
+ desc "Add regular one-to-many association (has_many / belongs_to)"
7
+ argument :name, type: :string,
8
+ desc: "The parent resource that has_many CHILDREN objects"
9
+ argument :children, type: :string,
10
+ desc: "The child resources that belongs_to the NAME object"
11
+ class_option :dependent, type: :string, banner: 'ACTION',
12
+ desc: 'Can be destroy, delete, or restrict'
13
+ class_option :nested, type: :array, banner: 'attribute [...]',
14
+ desc: 'Add accepts_nested_attributes_for (incl. whitelisting)'
15
+ class_option :inverse, type: :boolean, default: false,
16
+ desc: 'Add inverse_of to both models'
17
+ class_option :counter, type: :boolean, default: false,
18
+ desc: 'Add counter_cache for CHILDREN'
19
+ class_option :migration, type: :boolean, default: false,
20
+ desc: 'Create a migration for added attributes'
21
+ class_option :index, type: :boolean, default: true,
22
+ desc: 'Add an index to the child migration'
23
+ class_option :before, type: :array,
24
+ desc: 'Add a line before generated text in models'
25
+ class_option :after, type: :array,
26
+ desc: 'Add a line after generated text in models'
27
+ source_root File.expand_path('../templates', __FILE__)
28
+
29
+ def add_migration
30
+ return unless options.migration?
31
+ migration_template 'child_migration.rb', "db/migrate/#{migration_name}.rb"
32
+ end
33
+
34
+ def add_counter
35
+ return unless options.counter?
36
+ migration_template 'counter_migration.rb', "db/migrate/#{counter_migration}.rb"
37
+ end
38
+
39
+ def add_to_models
40
+ inject_into_class "app/models/#{name}.rb", class_name do
41
+ text = before_array.include?(name) ? "\n" : ""
42
+ text << " has_many :#{children}"
43
+ text << ", inverse_of: :#{name}" if options.inverse?
44
+ text << ", dependent: :#{dependent}" if options[:dependent]
45
+ text << "\n"
46
+ text << " accepts_nested_attributes_for :#{children}\n" if options[:nested]
47
+ text << "\n" if after_array.include?(name)
48
+ text
49
+ end
50
+
51
+ child = children.singularize
52
+ inject_into_class "app/models/#{child}.rb", child.camelize do
53
+ text = before_array.include?(child) ? "\n" : ""
54
+ text << " belongs_to :#{name}"
55
+ text << ", inverse_of: :#{children}" if options.inverse?
56
+ text << ", counter_cache: true" if options.counter?
57
+ text << "\n"
58
+ text << "\n" if after_array.include?(child)
59
+ text
60
+ end
61
+ end
62
+
63
+ def add_to_permit
64
+ return unless options[:nested]
65
+ list = options[:nested].map{|n| ":#{n}"}.join(', ')
66
+ text = "#{children}_attributes: [ #{list} ]"
67
+ file = "app/controllers/#{table_name}_controller.rb"
68
+ gsub_file file, /(permit\(.*)\)/, "\\1, #{text})"
69
+ # Special case: no previous permit
70
+ gsub_file file, /^(\s*params)\[:#{name}\]$/, "\\1.require(:#{name}).permit(#{text})"
71
+ end
72
+
73
+ protected
74
+
75
+ def before_array
76
+ options['before'] || []
77
+ end
78
+
79
+ def after_array
80
+ options['after'] || []
81
+ end
82
+
83
+ def dependent
84
+ if options[:dependent] && options[:dependent] == "restrict"
85
+ "restrict_with_exception"
86
+ else
87
+ options[:dependent]
88
+ end
89
+ end
90
+
91
+ def migration_name
92
+ "add_#{name}_id_to_#{children}"
93
+ end
94
+
95
+ def create_index?
96
+ options[:index]
97
+ end
98
+
99
+ def counter_migration
100
+ "add_#{children}_count_to_#{table_name}"
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,8 @@
1
+ class <%= migration_name.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ add_column :<%= children %>, :<%= name %>_id, :integer
4
+ <%- if create_index? -%>
5
+ add_index :<%= children %>, :<%= name %>_id
6
+ <%- end -%>
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ class <%= counter_migration.camelize %> < ActiveRecord::Migration
2
+ def change
3
+ add_column :<%= table_name %>, :<%= children %>_count, :integer
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module ScaffoldPlus
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1 @@
1
+ require "scaffold_plus/version"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'scaffold_plus/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "scaffold_plus"
8
+ spec.version = ScaffoldPlus::VERSION
9
+ spec.authors = ["Volker Wiegand"]
10
+ spec.email = ["volker.wiegand@cvw.de"]
11
+ spec.summary = "A collection of little helpers for Rails scaffolding"
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/volkerwiegand/scaffold_plus"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '>= 1.9.3'
22
+ spec.add_dependency 'activerecord', '~> 4.0'
23
+
24
+ spec.add_development_dependency 'railties', '~> 4.0'
25
+ spec.add_development_dependency "bundler", "~> 1.6"
26
+ spec.add_development_dependency "rake", "~> 10.1"
27
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scaffold_plus
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Volker Wiegand
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: railties
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.1'
69
+ description: A collection of little helpers for Rails scaffolding
70
+ email:
71
+ - volker.wiegand@cvw.de
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - Gemfile
77
+ - LICENSE.txt
78
+ - Makefile
79
+ - README.md
80
+ - Rakefile
81
+ - lib/generators/scaffold_plus/has_many/has_many_generator.rb
82
+ - lib/generators/scaffold_plus/has_many/templates/child_migration.rb
83
+ - lib/generators/scaffold_plus/has_many/templates/counter_migration.rb
84
+ - lib/scaffold_plus.rb
85
+ - lib/scaffold_plus/version.rb
86
+ - scaffold_plus.gemspec
87
+ homepage: https://github.com/volkerwiegand/scaffold_plus
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: 1.9.3
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.3.0
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: A collection of little helpers for Rails scaffolding
111
+ test_files: []