model_base_generators 0.2.4 → 0.3.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: 14ffe879896645ad2881f5d7cea65acff0493005
4
- data.tar.gz: e2e5796a4879d9081a16bedcb8c5babbedebabca
3
+ metadata.gz: b699cb53644860bfcf7b663b76ff234477311ea9
4
+ data.tar.gz: 10afb774b6eacb3704bfdbe3c46f522f1ec23e17
5
5
  SHA512:
6
- metadata.gz: f259992f73231c6d76d80bab1bd9c0fec301aec36accc1745a84c27ef354629c48e5955a2e4dce7c9b717e96aa3764b960898ab47486c3af286583d1d0d39b4d
7
- data.tar.gz: c38fa3e58ff852a795226ce9fca1496ace9b62493a1a13c295c8520ffab1e9c742698db24dba08a712d17d69b72a2924cd571200a5c90dbdbce254675f310b9e
6
+ metadata.gz: 65d880b9527c27cd54de406409522077a4b65feb27c7bf4212b94c123d274a86d2161eabbcd9c5f568d4a96cfbcbb7d5d29306b25f9b77218d23ea298a98507d
7
+ data.tar.gz: 4c29670bc96f3e895a4cc7a7b5c1debe1008be6e1245e8e8d79d7eb3741369921798bdbd5ba94ad3ea89ba22aad8f1001235e47770eba63722be82493b131013
data/README.md CHANGED
@@ -16,6 +16,10 @@ And then execute:
16
16
 
17
17
  $ bundle
18
18
 
19
+ ## Setup
20
+
21
+ $ bin/rails g model_base:install
22
+
19
23
  ## Usage
20
24
 
21
25
  ### 1. Generate your model
@@ -56,6 +60,9 @@ $ bin/rails g scaffold_controller issues
56
60
 
57
61
  Run scaffold_controller **WITHOUT attributes**!
58
62
 
63
+ ## Template for `rails new`
64
+
65
+ You can use `rails_template.rb` for `--template` option of `rails new`.
59
66
 
60
67
 
61
68
  ## Development
@@ -0,0 +1,19 @@
1
+ require 'rails/generators/base'
2
+
3
+ module ModelBase
4
+ class InstallGenerator < ::Rails::Generators::Base
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ desc "Generate files to work with model_base"
8
+
9
+ def generate_files
10
+ [
11
+ 'app/controllers/concerns/authentication.rb',
12
+ 'spec/factories/users.rb',
13
+ 'spec/support/controller_macros.rb',
14
+ 'spec/support/devise.rb',
15
+ 'spec/support/field_assertions.rb',
16
+ ].each{|f| template f, f }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ module Authentication
2
+ def self.included(klass)
3
+ klass.class_eval do
4
+ before_action :authenticate_user!
5
+ around_action :set_current_user
6
+ end
7
+ end
8
+
9
+ def set_current_user(&block)
10
+ User.current(current_user, &block)
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ FactoryGirl.define do
2
+ factory :user do
3
+ email "user1@example.com"
4
+ before(:create) do |u|
5
+ u.password = u.password_confirmation = "password"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,18 @@
1
+ module ControllerMacros
2
+ def devise_login(key, user)
3
+ @request.env["devise.mapping"] = Devise.mappings[key]
4
+ sign_in(user)
5
+ end
6
+
7
+ def devise_user_login(user)
8
+ devise_login(:user, user)
9
+ end
10
+
11
+ def login_admin
12
+ before(:each){ devise_login(:admin, FactoryGirl.create(:admin)) }
13
+ end
14
+
15
+ def login_user
16
+ before(:each){ devise_login(:user, FactoryGirl.create(:user)) }
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ # https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec)
2
+ RSpec.configure do |config|
3
+ config.with_options(:type => :controller) do |c|
4
+ c.include Devise::Test::ControllerHelpers
5
+ c.include ControllerMacros
6
+ c.extend ControllerMacros
7
+ end
8
+
9
+ # For request spec
10
+ # https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara
11
+ # http://qiita.com/gakkie/items/40e5678af7b63afc14df
12
+ config.include Warden::Test::Helpers, :type => :request
13
+ config.before :suite do
14
+ Warden.test_mode!
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ def assert_select_datetime_field(object, field, n = 5)
2
+ (1..n).each do |i|
3
+ assert_select "select##{object}_#{field}_#{i}i[name=?]", "#{object}[#{field}(#{i}i)]"
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module ModelBase
2
- VERSION = "0.2.4"
2
+ VERSION = "0.3.0"
3
3
  end
data/rails_template.rb ADDED
@@ -0,0 +1,289 @@
1
+ # coding: utf-8
2
+ # http://guides.rubyonrails.org/rails_application_templates.html
3
+ # https://github.com/morizyun/rails4_template/blob/master/app_template.rb
4
+ # https://github.com/search?q=rails%20template&source=c
5
+ #
6
+ # The method "insert_to_file" is defined by thor instead of rails
7
+ # https://github.com/erikhuda/thor/blob/master/lib/thor/actions/inject_into_file.rb
8
+
9
+ require 'bundler'
10
+ require 'fileutils'
11
+
12
+ def git_add_commit(msg, path = '.')
13
+ git add: path
14
+ raise "Failed to git add #{path.inspect} with exit code #{$?.inspect}" unless $? == 0
15
+ git commit: "-m '#{msg}'"
16
+ raise "Failed to git commit #{msg.inspect} with exit code #{$?.inspect}" unless $? == 0
17
+ end
18
+
19
+ def generate_with_git(arg)
20
+ generate arg, '-f'
21
+ git_add_commit "rails generate #{arg}"
22
+ end
23
+
24
+ def git_run(cmd)
25
+ run cmd
26
+ raise "Failed to run: #{cmd}" unless $? == 0
27
+ git_add_commit cmd
28
+ end
29
+
30
+ def download_file(dest, url)
31
+ FileUtils.mkdir_p File.dirname(dest)
32
+ git_run "curl #{url} -o #{dest}"
33
+ end
34
+
35
+ def git_rake(*args)
36
+ git_run "bin/rake %s" % args.join(" ")
37
+ end
38
+
39
+ def uncomment(path, target)
40
+ text = IO.binread(path)
41
+ text.sub!(/\#\s*#{Regexp.escape(target)}/, target)
42
+ IO.binwrite(path, text)
43
+ end
44
+
45
+ git :init unless ENV['SKIP_GIT_INIT'] =~ /true|yes|on|1/i
46
+ git_add_commit "#{File.basename($PROGRAM_NAME)} #{ARGV.join(' ')}"
47
+
48
+ if File.exist?('README.rdoc') && File.exist?('README.md')
49
+ run 'mv README.rdoc README.md'
50
+ git_add_commit "Rename README.rdoc to README.md"
51
+ end
52
+
53
+ ## Gemfile
54
+
55
+ gem "twitter-bootstrap-rails"
56
+
57
+ gem 'devise'
58
+
59
+ # https://github.com/CanCanCommunity/cancancan
60
+ gem 'cancancan'
61
+
62
+ # https://github.com/brainspec/enumerize
63
+ gem 'enumerize'
64
+
65
+ gem "kaminari"
66
+
67
+ # # https://github.com/brainspec/enumerize
68
+ # gem 'enumerize'
69
+
70
+ # https://github.com/sinsoku/pretty_validation
71
+ # http://sinsoku.hatenablog.com/entry/2015/11/15/103924
72
+ gem 'pretty_validation', git: "https://github.com/akm/pretty_validation.git"
73
+
74
+ # # https://github.com/sferik/rails_admin
75
+ # gem 'rails_admin' unless ENV['DISABLE_RAILS_ADMIN'] =~ /true|yes|on|1/i
76
+
77
+ # Use dotenv to load environment variables
78
+ gem 'dotenv-rails', :require => 'dotenv/rails-now'
79
+
80
+ gem_group :development, :test do
81
+ gem "rspec"
82
+ gem "rspec-rails"
83
+ gem 'simplecov' , require: false
84
+ gem 'simplecov-rcov', require: false
85
+ gem "pry-rails"
86
+ gem "pry-byebug"
87
+ gem "pry-stack_explorer"
88
+ gem "fuubar"
89
+ gem "factory_girl"
90
+ gem "factory_girl_rails"
91
+ gem "annotate"
92
+ gem "rails_best_practices"
93
+ # https://github.com/flyerhzm/bullet
94
+ gem 'bullet'
95
+ end
96
+
97
+ gem_group :development do
98
+ gem "better_errors"
99
+ gem 'binding_of_caller'
100
+ end
101
+
102
+
103
+ gem_group :test do
104
+ gem 'rails-controller-testing'
105
+ end
106
+
107
+ git_add_commit 'Add gems to Gemfile'
108
+
109
+ Bundler.with_clean_env do
110
+ run 'bundle install'
111
+ end
112
+
113
+ git_add_commit 'bundle install'
114
+
115
+ # set config/application.rb
116
+ application do
117
+ %q{
118
+ config.time_zone = 'Tokyo'
119
+ config.active_record.default_timezone = :local
120
+
121
+ config.i18n.default_locale = :ja
122
+
123
+ config.generators do |g|
124
+ # g.orm :mongoid
125
+ g.test_framework :rspec
126
+ g.factory_girl dir: 'spec/factories'
127
+ # g.template_engine :haml
128
+ end
129
+ }
130
+ end
131
+
132
+ git_add_commit 'Add settings of timezone, locale and generators'
133
+
134
+ # Rails Japanese locale
135
+ download_file "config/locales/ja.yml", "https://raw.githubusercontent.com/svenfuchs/rails-i18n/master/rails/locale/ja.yml"
136
+
137
+ ## rspec
138
+ generate_with_git 'rspec:install'
139
+ download_file ".rspec", "https://raw.githubusercontent.com/akm/rails_template/master/.rspec"
140
+
141
+ uncomment('spec/rails_helper.rb', "Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }")
142
+ git_add_commit 'Enable rb files under spec/support'
143
+
144
+
145
+
146
+ ## simplecov
147
+ insert_into_file 'spec/rails_helper.rb', <<EOS, before: "# This file is copied to spec/ when you run 'rails generate rspec:install'"
148
+ # https://github.com/colszowka/simplecov
149
+ require 'simplecov'
150
+ SimpleCov.start 'rails'
151
+
152
+ EOS
153
+ git_add_commit 'Enable simplecov'
154
+
155
+
156
+ ## twitter-bootstrap-rails
157
+ generate_with_git 'bootstrap:install static'
158
+ generate_with_git 'bootstrap:layout'
159
+
160
+ ## kaminari
161
+ generate_with_git 'kaminari:views bootstrap3'
162
+
163
+ ## Devise
164
+ generate_with_git 'devise:install'
165
+ download_file "config/locales/ja.devise.yml", "https://gist.githubusercontent.com/kawamoto/4729292/raw/80fd53034289be926de2e2206c2ab7afbac35993/devise.ja.yml"
166
+
167
+ # Flash messages are included in bootstrap views
168
+ # insert_into_file 'app/views/layouts/application.html.erb', <<EOS , after: '<body>'
169
+ # <p class="notice"><%= notice %></p>
170
+ # <p class="alert"><%= alert %></p>
171
+ #
172
+ # EOS
173
+
174
+ insert_into_file 'config/environments/development.rb', <<EOS, after: 'config.action_mailer.raise_delivery_errors = false'
175
+
176
+ # for devise
177
+ config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
178
+ EOS
179
+
180
+
181
+ root_path = (ENV['DISABLE_RAILS_ADMIN'] =~ /true|yes|on|1/i) ? 'welcome#index' : 'rails_admin/main#dashboard'
182
+ insert_into_file 'config/routes.rb', <<"EOS", after: 'Rails.application.routes.draw do'
183
+
184
+ # root to: "devise/sessions#new" # Sign in
185
+ root to: "#{root_path}" # TODO Change top page
186
+
187
+ EOS
188
+ git_add_commit 'Following instructions of devise:install'
189
+
190
+ generate_with_git 'devise User'
191
+
192
+ insert_into_file 'app/models/user.rb', <<EOS, before: 'end'
193
+
194
+ class << self
195
+ def current_user=(user)
196
+ Thread.current[:current_user] = user
197
+ end
198
+
199
+ def current_user
200
+ Thread.current[:current_user]
201
+ end
202
+
203
+ def current(user)
204
+ orig_user, User.current_user = User.current_user, user
205
+ begin
206
+ return yield
207
+ ensure
208
+ User.current_user = orig_user
209
+ end
210
+ end
211
+ end
212
+
213
+ EOS
214
+ git_add_commit 'Add User.current_user'
215
+
216
+
217
+ # https://github.com/plataformatec/devise/wiki/I18n#japanese-devisejayml
218
+ download_file "config/locales/ja.devise.yml", "https://gist.githubusercontent.com/satour/6c15f27211fdc0de58b4/raw/d4b5815295c65021790569c9be447d15760f4957/devise.ja.yml"
219
+
220
+ insert_into_file 'spec/rails_helper.rb', <<EOS, after: '# Add additional requires below this line. Rails is not loaded until this point!'
221
+
222
+ # For spec/controllers
223
+ # https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec)
224
+ require 'devise'
225
+
226
+ # For spec/requests
227
+ # https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara
228
+
229
+ EOS
230
+ git_add_commit 'Load devise on rails_helper'
231
+
232
+ ## cancancan
233
+ generate_with_git 'cancan:ability'
234
+ insert_into_file 'app/models/ability.rb', <<EOS, after: ' # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities'
235
+
236
+ can :manage, :all
237
+ EOS
238
+
239
+ ## bullet
240
+ insert_into_file 'config/environments/development.rb', <<EOS, after: " # config.action_view.raise_on_missing_translations = true"
241
+
242
+ config.after_initialize do
243
+ Bullet.enable = true
244
+ Bullet.alert = false
245
+ Bullet.bullet_logger = true
246
+ Bullet.console = true
247
+ # Bullet.growl = true
248
+ # Bullet.xmpp = { :account => 'bullets_account@jabber.org',
249
+ # :password => 'bullets_password_for_jabber',
250
+ # :receiver => 'your_account@jabber.org',
251
+ # :show_online_status => true }
252
+ Bullet.rails_logger = true
253
+ # Bullet.honeybadger = true
254
+ # Bullet.bugsnag = true
255
+ # Bullet.airbrake = true
256
+ # Bullet.rollbar = true
257
+ # Bullet.add_footer = true
258
+ # Bullet.stacktrace_includes = [ 'your_gem', 'your_middleware' ]
259
+ # Bullet.stacktrace_excludes = [ 'their_gem', 'their_middleware' ]
260
+ # Bullet.slack = { webhook_url: 'http://some.slack.url', foo: 'bar' }
261
+ end
262
+
263
+ EOS
264
+ git_add_commit 'Add config for bullet'
265
+
266
+
267
+ # ## rails_admin
268
+ # unless ENV['DISABLE_RAILS_ADMIN'] =~ /true|yes|on|1/i
269
+ # generate_with_git 'rails_admin:install'
270
+ # download_file "config/locales/ja.rails_admin.yml", "https://raw.githubusercontent.com/starchow/rails_admin-i18n/master/locales/ja.yml"
271
+ # end
272
+
273
+ ## model_base_generators
274
+ generate_with_git 'model_base:install'
275
+
276
+
277
+ ## DB
278
+ git_rake "db:create db:migrate"
279
+
280
+ ## gitguard
281
+ gem_group :development do
282
+ # https://github.com/akm/gitguard
283
+ gem 'gitguard'
284
+ end
285
+
286
+ generate_with_git "gitguard:install"
287
+
288
+ ## The End
289
+ git commit: "-m \"\[COMPLETE\] rails new with akm/rails_template\" --allow-empty "
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_base_generators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - akm
@@ -270,6 +270,12 @@ files:
270
270
  - example/spec/views/projects/new.html.erb_spec.rb
271
271
  - example/spec/views/projects/show.html.erb_spec.rb
272
272
  - example/tmp/.keep
273
+ - lib/generators/model_base/install_generator.rb
274
+ - lib/generators/model_base/templates/app/controllers/concerns/authentication.rb
275
+ - lib/generators/model_base/templates/spec/factories/users.rb
276
+ - lib/generators/model_base/templates/spec/support/controller_macros.rb
277
+ - lib/generators/model_base/templates/spec/support/devise.rb
278
+ - lib/generators/model_base/templates/spec/support/field_assertions.rb
273
279
  - lib/model_base.rb
274
280
  - lib/model_base/column_attribute.rb
275
281
  - lib/model_base/config.rb
@@ -299,6 +305,7 @@ files:
299
305
  - lib/templates/rspec/scaffold/routing_spec.rb
300
306
  - lib/templates/rspec/scaffold/show_spec.rb
301
307
  - model_base_generators.gemspec
308
+ - rails_template.rb
302
309
  homepage: https://github.com/akm/model_base_generators
303
310
  licenses:
304
311
  - MIT