models_generator 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/Guardfile +13 -0
- data/LICENSE.txt +22 -0
- data/README.md +80 -0
- data/Rakefile +1 -0
- data/lib/generators/master_cache/config_generator.rb +14 -0
- data/lib/generators/master_cache/templates/models_generator_config.rb +4 -0
- data/lib/generators/rails/models_generator.rb +42 -0
- data/lib/generators/rails/templates/model.rb.erb +11 -0
- data/lib/generators/rails/templates/model_spec.rb.erb +6 -0
- data/lib/models_generator/config.rb +45 -0
- data/lib/models_generator/version.rb +3 -0
- data/lib/models_generator.rb +2 -0
- data/models_generator.gemspec +29 -0
- data/spec/models_generator/config_spec.rb +38 -0
- data/spec/spec_helper.rb +3 -0
- metadata +184 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec', :cli => '--color --format s', :all_on_start => false, :all_after_pass => false do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec/" }
|
8
|
+
end
|
9
|
+
|
10
|
+
guard 'bundler' do
|
11
|
+
watch('Gemfile')
|
12
|
+
watch(/^.+\.gemspec/)
|
13
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 DianthuDia
|
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/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# ModelsGenerator
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/DianthuDia/models_generator.png)](http://travis-ci.org/DianthuDia/models_generator) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/DianthuDia/models_generator)
|
4
|
+
|
5
|
+
A model files generator plugin from migrate file
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'models_generator', :groups => :development
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install models_generator
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Basics
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class CreateAllTables < ActiveRecord::Migration
|
27
|
+
def self.up
|
28
|
+
create_table(:employees) {|t| t.string :name}
|
29
|
+
create_table(:users) {|t| t.string :name}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
```bash
|
35
|
+
$ rake db:migrate
|
36
|
+
$ rails g models # NOT 'model' !
|
37
|
+
create app/models/employee.rb
|
38
|
+
create spec/models/employee_spec.rb
|
39
|
+
create app/models/user.rb
|
40
|
+
create spec/models/user_spec.rb
|
41
|
+
```
|
42
|
+
|
43
|
+
### General configuration options
|
44
|
+
|
45
|
+
You can configure the following default values by overriding these values using `ModelsGenerator.configure` method.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
master_attrs # %w[name label value] by default
|
49
|
+
master_eval # '' by default. If have 'master_cache' gem, 'master_cache'
|
50
|
+
```
|
51
|
+
|
52
|
+
If the table have value of master_attrs, model file include master_eval. For instance:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
class EmployeeType < ActiveRecord::Base
|
56
|
+
master_cache
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
There's a handy generator that generates the default configuration file into config/initializers directory.
|
61
|
+
Run the following generator command, then edit the generated file.
|
62
|
+
|
63
|
+
`$ rails g models_generator:config`
|
64
|
+
|
65
|
+
## Supported versions
|
66
|
+
|
67
|
+
* Ruby 1.8.7, 1.9.3
|
68
|
+
* ActiveRecord 3.x
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
1. Fork it
|
73
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
74
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
75
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
76
|
+
5. Create new Pull Request
|
77
|
+
|
78
|
+
## License
|
79
|
+
|
80
|
+
MIT License. Copyright 2012 DianthuDia.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module ModelsGenerator::Generators
|
2
|
+
class ConfigGenerator < Rails::Generators::Base
|
3
|
+
source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
4
|
+
|
5
|
+
desc <<DESC
|
6
|
+
Description:
|
7
|
+
Copies models_generator configuration file to your application's initializer directory.
|
8
|
+
DESC
|
9
|
+
|
10
|
+
def copy_config_file
|
11
|
+
template 'models_generator_config.rb', 'config/initializers/models_generator_config.rb'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'rails/generators'
|
4
|
+
require 'rails/generators/active_record/model/model_generator'
|
5
|
+
|
6
|
+
class Rails::ModelsGenerator < ActiveRecord::Generators::ModelGenerator
|
7
|
+
source_root File.expand_path('../templates', __FILE__)
|
8
|
+
|
9
|
+
# to use parent generator without args
|
10
|
+
remove_argument :name
|
11
|
+
def initialize(args, *options)
|
12
|
+
self.name = args.pop || 'base'
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
16
|
+
# don't create migration file
|
17
|
+
def create_migration_file; end
|
18
|
+
|
19
|
+
def create_model_file
|
20
|
+
tables = ActiveRecord::Base.connection.tables - [ActiveRecord::Migrator.schema_migrations_table_name]
|
21
|
+
|
22
|
+
tables.each do |tbl_name|
|
23
|
+
clear_instance_variables pluralize_table_names? ? tbl_name.singularize : tbl_name
|
24
|
+
template 'model.rb.erb', File.join('app/models', class_path, "#{file_name}.rb")
|
25
|
+
template 'model_spec.rb.erb', File.join('spec/models', class_path, "#{file_name}_spec.rb")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
remove_hook_for :test_framework
|
30
|
+
|
31
|
+
protected
|
32
|
+
def master?
|
33
|
+
columns = ActiveRecord::Base.connection.columns(table_name).map(&:name)
|
34
|
+
ModelsGenerator.config.master_attrs.all? {|c| columns.include? c}
|
35
|
+
end
|
36
|
+
|
37
|
+
# clear memoization without @options
|
38
|
+
def clear_instance_variables(name)
|
39
|
+
instance_variables.each{|v| remove_instance_variable v unless v.eql? :@options}
|
40
|
+
initialize [name], @options
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
<% module_namespacing do -%>
|
3
|
+
class <%= class_name %> < <%= parent_class_name.classify %>
|
4
|
+
<% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
|
5
|
+
belongs_to :<%= attribute.name %>
|
6
|
+
<% end -%>
|
7
|
+
<% if master? -%>
|
8
|
+
<%= ModelsGenerator.config.master_eval %>
|
9
|
+
<% end -%>
|
10
|
+
end
|
11
|
+
<% end -%>
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'active_support/configurable'
|
2
|
+
|
3
|
+
module ModelsGenerator
|
4
|
+
# Configures global settings for ModelsGenerator
|
5
|
+
# ModelsGenerator.configure do |config|
|
6
|
+
# config.all_name = :instances
|
7
|
+
# end
|
8
|
+
def self.configure(&block)
|
9
|
+
yield @config ||= ModelsGenerator::Configuration.new
|
10
|
+
end
|
11
|
+
|
12
|
+
# Global settings for ModelsGenerator
|
13
|
+
def self.config
|
14
|
+
@config
|
15
|
+
end
|
16
|
+
|
17
|
+
# need a Class for 3.0
|
18
|
+
class Configuration #:nodoc:
|
19
|
+
include ActiveSupport::Configurable
|
20
|
+
config_accessor :master_attrs
|
21
|
+
config_accessor :master_eval
|
22
|
+
|
23
|
+
def param_name
|
24
|
+
config.param_name.respond_to?(:call) ? config.param_name.call : config.param_name
|
25
|
+
end
|
26
|
+
|
27
|
+
# define param_name writer (copied from AS::Configurable)
|
28
|
+
writer, line = 'def param_name=(value); config.param_name = value; end', __LINE__
|
29
|
+
singleton_class.class_eval writer, __FILE__, line
|
30
|
+
class_eval writer, __FILE__, line
|
31
|
+
end
|
32
|
+
|
33
|
+
# default
|
34
|
+
configure do |config|
|
35
|
+
begin
|
36
|
+
require 'master_cache'
|
37
|
+
rescue LoadError
|
38
|
+
#do nothing
|
39
|
+
end
|
40
|
+
|
41
|
+
config.master_attrs = %w[name label value]
|
42
|
+
config.master_eval = defined?(MasterCache)? 'master_cache' : ''
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'models_generator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "models_generator"
|
8
|
+
gem.version = ModelsGenerator::VERSION
|
9
|
+
gem.authors = ["DianthuDia"]
|
10
|
+
gem.email = ["dianthudia@y7mail.com"]
|
11
|
+
gem.description = "A model files generator plugin from migrate file"
|
12
|
+
gem.summary = "models_generator generate app/modes/*.rb from migration file"
|
13
|
+
gem.homepage = "https://github.com/DianthuDia/models_generator"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency 'activerecord', '>= 3.0.0'
|
21
|
+
|
22
|
+
gem.add_development_dependency 'rake'
|
23
|
+
gem.add_development_dependency 'rspec'
|
24
|
+
gem.add_development_dependency 'guard'
|
25
|
+
gem.add_development_dependency 'rb-fsevent' if RUBY_PLATFORM.include? 'darwin'
|
26
|
+
gem.add_development_dependency 'rb-inotify' if RUBY_PLATFORM.include? 'linux'
|
27
|
+
gem.add_development_dependency 'guard-bundler'
|
28
|
+
gem.add_development_dependency 'guard-rspec'
|
29
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ModelsGenerator::Configuration do
|
4
|
+
shared_examples_for 'a configure object' do
|
5
|
+
context 'by default' do
|
6
|
+
it { should eq default }
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'configured via config block' do
|
10
|
+
before do
|
11
|
+
ModelsGenerator.configure {|c| c.send "#{config_name}=", other}
|
12
|
+
end
|
13
|
+
|
14
|
+
it { should eq other }
|
15
|
+
|
16
|
+
after do
|
17
|
+
ModelsGenerator.configure {|c| c.send "#{config_name}=", default}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#master_attrs' do
|
23
|
+
subject { ModelsGenerator.config.master_attrs}
|
24
|
+
let(:config_name) { :master_attrs }
|
25
|
+
let(:default) { %w[name label value] }
|
26
|
+
let(:other) { %w[name position] }
|
27
|
+
it_behaves_like 'a configure object'
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#master_eval' do
|
31
|
+
subject { ModelsGenerator.config.master_eval}
|
32
|
+
let(:config_name) { :master_eval }
|
33
|
+
let(:default) { '' }
|
34
|
+
let(:other) { 'validate :name, :presence => true' }
|
35
|
+
it_behaves_like 'a configure object'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: models_generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- DianthuDia
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: guard
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rb-fsevent
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: guard-bundler
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: guard-rspec
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
description: A model files generator plugin from migrate file
|
127
|
+
email:
|
128
|
+
- dianthudia@y7mail.com
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- .gitignore
|
134
|
+
- .rspec
|
135
|
+
- .travis.yml
|
136
|
+
- Gemfile
|
137
|
+
- Guardfile
|
138
|
+
- LICENSE.txt
|
139
|
+
- README.md
|
140
|
+
- Rakefile
|
141
|
+
- lib/generators/master_cache/config_generator.rb
|
142
|
+
- lib/generators/master_cache/templates/models_generator_config.rb
|
143
|
+
- lib/generators/rails/models_generator.rb
|
144
|
+
- lib/generators/rails/templates/model.rb.erb
|
145
|
+
- lib/generators/rails/templates/model_spec.rb.erb
|
146
|
+
- lib/models_generator.rb
|
147
|
+
- lib/models_generator/config.rb
|
148
|
+
- lib/models_generator/version.rb
|
149
|
+
- models_generator.gemspec
|
150
|
+
- spec/models_generator/config_spec.rb
|
151
|
+
- spec/spec_helper.rb
|
152
|
+
homepage: https://github.com/DianthuDia/models_generator
|
153
|
+
licenses: []
|
154
|
+
post_install_message:
|
155
|
+
rdoc_options: []
|
156
|
+
require_paths:
|
157
|
+
- lib
|
158
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
159
|
+
none: false
|
160
|
+
requirements:
|
161
|
+
- - ! '>='
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
164
|
+
segments:
|
165
|
+
- 0
|
166
|
+
hash: -3608145962022338579
|
167
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
168
|
+
none: false
|
169
|
+
requirements:
|
170
|
+
- - ! '>='
|
171
|
+
- !ruby/object:Gem::Version
|
172
|
+
version: '0'
|
173
|
+
segments:
|
174
|
+
- 0
|
175
|
+
hash: -3608145962022338579
|
176
|
+
requirements: []
|
177
|
+
rubyforge_project:
|
178
|
+
rubygems_version: 1.8.24
|
179
|
+
signing_key:
|
180
|
+
specification_version: 3
|
181
|
+
summary: models_generator generate app/modes/*.rb from migration file
|
182
|
+
test_files:
|
183
|
+
- spec/models_generator/config_spec.rb
|
184
|
+
- spec/spec_helper.rb
|