fetty-generators 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,6 @@
1
- require 'rubygems'
2
- require 'rubygems/command.rb'
3
- require 'rubygems/dependency_installer.rb'
4
1
  require 'rails/generators/base'
5
2
  require 'bundler'
3
+ require 'bundler/dsl'
6
4
 
7
5
  module Fetty
8
6
  module Generators
@@ -18,24 +16,9 @@ module Fetty
18
16
 
19
17
  protected
20
18
 
21
- def add_gem(name, options = {})
22
- unless gemfile_included? name
23
- gemfile = File.expand_path(destination_path("Gemfile"), __FILE__)
24
- gemfile_content = File.read(gemfile)
25
- File.open(gemfile, 'a') { |f| f.write("\n") } unless gemfile_content =~ /\n\Z/
26
-
27
- gem name, options
28
-
29
- if bundle_need_refresh?
30
- install_gem(name,options)
31
- end
32
- end
33
- rescue Exception => e
34
- raise e
35
- end
36
-
37
- def gemfile_included?(name)
38
- file_contains?("Gemfile",name)
19
+ def add_gem(&block)
20
+ yield
21
+ refresh_bundle
39
22
  rescue Exception => e
40
23
  raise e
41
24
  end
@@ -56,6 +39,13 @@ protected
56
39
  File.directory? path
57
40
  end
58
41
 
42
+ def class_exists?(class_name)
43
+ klass = Rails.application.class.parent_name.constantize.const_get(class_name)
44
+ return klass.is_a?(Class)
45
+ rescue NameError
46
+ return false
47
+ end
48
+
59
49
  def destroy(path)
60
50
  begin
61
51
  if file_exists?(path)
@@ -86,9 +76,13 @@ protected
86
76
  end
87
77
 
88
78
  def print_notes(message,notes = "notes",color = :yellow)
89
- puts '', '='*80
90
- say_status "#{notes}", "#{message}", color
91
- puts '='*80, ''; sleep 0.5
79
+ unless message.blank?
80
+ puts '', '='*80
81
+ say_status "#{notes}", "#{message}", color
82
+ puts '='*80, ''; sleep 0.5
83
+ else
84
+ puts "\n"
85
+ end
92
86
  end
93
87
 
94
88
  def print_usage
@@ -96,61 +90,65 @@ protected
96
90
  exit
97
91
  end
98
92
 
99
- def install_gem(name,options = {})
100
- print_notes("Installing #{name}")
93
+ def install_local_gem(name,version = nil)
101
94
  ::Bundler.with_clean_env do
102
- unless options.empty?
103
- system("gem install #{name} -v=#{options}")
95
+ if version
96
+ `gem install #{name} -v=#{version}`
104
97
  else
105
- system("gem install #{name}")
98
+ `gem install #{name}`
106
99
  end
107
100
  end
101
+ $? == 0 ? true : false
108
102
  rescue Exception => e
109
103
  raise e
110
104
  end
111
105
 
112
- def refresh_bundle
113
- print_notes('Refresh bundle')
106
+ def check_local_gem?(name,version = nil)
114
107
  ::Bundler.with_clean_env do
115
- `bundle install`
108
+ if version
109
+ `gem list #{name} -i -v=#{version}`
110
+ else
111
+ `gem list #{name} -i`
112
+ end
116
113
  end
114
+ $? == 0 ? true : false
117
115
  rescue Exception => e
118
116
  raise e
119
117
  end
120
118
 
121
- def bundle_need_refresh?
119
+ def refresh_bundle
122
120
  ::Bundler.with_clean_env do
123
- `bundle check`
121
+ `bundle`
124
122
  end
125
- $? == 0 ? false : true
126
123
  rescue Exception => e
127
124
  raise e
128
125
  end
129
126
 
130
- def file_contains?(filename,check_string)
131
- file = File.expand_path(destination_path(filename), __FILE__)
132
- if File.exist?(file)
133
- file_content = File.read(file)
134
- file_content.include?(check_string) ? true : false
135
- else
136
- false
127
+ def set_application_config(&block)
128
+ inject_into_class "config/application.rb", "Application" do
129
+ yield
137
130
  end
138
131
  rescue Exception => e
139
- raise e
132
+ raise e
140
133
  end
141
134
 
142
135
  def must_load_lib_directory
143
- unless file_contains?("config/application.rb",'config.autoload_paths += %W(#{config.root}/lib)')
144
- inject_into_file "config/application.rb", :after => "Rails::Application" do
145
- "\n\t\t" + 'config.autoload_paths += %W(#{config.root}/lib)'
146
- end
136
+ set_application_config do
137
+ ' config.autoload_paths += %W(#{config.root}/lib)' + "\n"
138
+ end
139
+ end
140
+
141
+ def gemfile_included?(name)
142
+ ::Bundler.with_clean_env do
143
+ `bundle show #{name}`
147
144
  end
145
+ $?.exitstatus == 0 ? true : false
148
146
  rescue Exception => e
149
147
  raise e
150
148
  end
151
149
 
152
150
  def using_cancan?
153
- gemfile_included?("cancan") && file_exists?("app/models/ability.rb")
151
+ gemfile_included?("cancan") && class_exists?("Ability")
154
152
  rescue Exception => e
155
153
  raise e
156
154
  end
@@ -174,15 +172,24 @@ protected
174
172
  end
175
173
 
176
174
  def using_fetty_authentication?
177
- file_exists?("app/controllers/users_controller.rb") &&
178
- file_exists?("app/controllers/sessions_controller.rb") &&
179
- file_exists?("app/controllers/reset_passwords_controller.rb") &&
180
- file_exists?("lib/users_authentication.rb")
175
+ class_exists?("UsersController") &&
176
+ class_exists?("SessionsController") &&
177
+ class_exists?("ResetPasswordsController") &&
178
+ class_exists?("User")
181
179
  rescue Exception => e
182
180
  raise e
183
181
  end
184
182
 
183
+ def check_required_gems?(*names)
184
+ names.each do |name|
185
+ return false unless gemfile_included? name
186
+ end
187
+ true
188
+ rescue Exception => e
189
+ raise e
190
+ end
185
191
 
186
192
  end
187
193
  end
188
194
  end
195
+
@@ -16,7 +16,7 @@ module Fetty
16
16
  else
17
17
  unless using_fetty_authentication?
18
18
  @orm = using_mongoid? ? 'mongoid' : 'active_record'
19
- add_gem("bcrypt-ruby", :require => "bcrypt")
19
+ add_gem { gem "bcrypt-ruby", :require => "bcrypt" }
20
20
  generate_users
21
21
  generate_sessions
22
22
  generate_reset_passwords
@@ -21,7 +21,7 @@ module Fetty
21
21
  if file_exists?(@model_path)
22
22
  unless using_mongoid?
23
23
  @orm = using_mongoid? ? 'mongoid' : 'active_record'
24
- using_mongoid? ? add_gem("mongoid-ancestry") : add_gem("ancestry")
24
+ add_gem { gem "ancestry" }
25
25
  copy_models_and_migrations
26
26
  copy_controller_and_helper
27
27
  copy_views
@@ -25,29 +25,32 @@ module Fetty
25
25
  print_usage unless scaffold_name.underscore =~ /^[a-z][a-z0-9_\/]+$/ && !attributes.empty?
26
26
  print_usage unless attributes.drop_while { |arg| arg.include?(':') }.count == 0
27
27
 
28
- @orm = using_mongoid? ? 'mongoid' : 'active_record'
29
-
30
- setting_model_attributes
31
- if options[:model]
32
- generate_model
33
- if options.migration? && !using_mongoid?
34
- generate_migration
35
- end
36
- end
37
-
38
- if options.controller?
39
- setting_controller_attributes
40
- generate_controller
41
- generate_helper
42
- generate_views
43
- generate_routes
44
- end
45
-
46
- if options.test?
47
- generate_test_unit if using_test_unit?
48
- generate_specs if using_rspec?
28
+ if check_required_gems? "jquery-rails", "simple_form", "kaminari", "ckeditor", "carrierwave"
29
+ @orm = using_mongoid? ? 'mongoid' : 'active_record'
30
+
31
+ setting_model_attributes
32
+ if options[:model]
33
+ generate_model
34
+ if options.migration? && !using_mongoid?
35
+ generate_migration
36
+ end
37
+ end
38
+
39
+ if options.controller?
40
+ setting_controller_attributes
41
+ generate_controller
42
+ generate_helper
43
+ generate_views
44
+ generate_routes
45
+ end
46
+
47
+ if options.test?
48
+ generate_test_unit if using_test_unit?
49
+ generate_specs if using_rspec?
50
+ end
51
+ else
52
+ raise "Missing gems: jquery-rails, simple_form, kaminari, ckeditor, carrierwave"
49
53
  end
50
-
51
54
  rescue Exception => e
52
55
  print_notes(e.message,"error",:red)
53
56
  end
@@ -28,6 +28,8 @@ module Fetty
28
28
  end
29
29
  remove_file 'public/index.html' if file_exists?('public/index.html')
30
30
  remove_file 'public/images/rails.png' if file_exists?('public/images/rails.png')
31
+ print_notes("Refreshing Bundle")
32
+ refresh_bundle
31
33
  rescue Exception => e
32
34
  print_notes(e.message,"error",:red)
33
35
  end
@@ -35,15 +37,18 @@ module Fetty
35
37
  private
36
38
 
37
39
  def setup_mongoid
38
- add_gem("bson_ext")
39
- add_gem("mongoid")
40
+ add_gem do
41
+ gem "bson_ext"
42
+ gem "mongoid"
43
+ end
40
44
  generate("mongoid:config")
45
+ set_application_config { " config.mongoid.preload_models = true\n" }
41
46
  rescue Exception => e
42
47
  raise e
43
48
  end
44
49
 
45
50
  def setup_cancan
46
- add_gem("cancan")
51
+ add_gem { gem "cancan" }
47
52
  copy_file 'ability.rb', 'app/models/ability.rb'
48
53
  inject_into_class 'app/controllers/application_controller.rb', ApplicationController do
49
54
  " rescue_from CanCan::AccessDenied do |exception| flash[:alert] = exception.message; redirect_to root_url end;\n"
@@ -51,24 +56,26 @@ private
51
56
  rescue Exception => e
52
57
  raise e
53
58
  end
54
-
59
+
55
60
  def setup_jquery_rails
56
- add_gem("jquery-rails")
61
+ add_gem { gem "jquery-rails" }
57
62
  generate("jquery:install")
58
63
  rescue Exception => e
59
64
  raise e
60
65
  end
61
66
 
62
67
  def setup_simple_form
63
- add_gem("simple_form")
68
+ add_gem { gem "simple_form" }
64
69
  generate("simple_form:install")
65
70
  rescue Exception => e
66
71
  raise e
67
72
  end
68
73
 
69
74
  def setup_carrierwave
70
- add_gem("mini_magick")
71
- add_gem("carrierwave")
75
+ add_gem do
76
+ gem "mini_magick"
77
+ gem "carrierwave"
78
+ end
72
79
  copy_file 'image_uploader.rb', 'app/uploaders/image_uploader.rb'
73
80
  copy_file 'file_uploader.rb', 'app/uploaders/file_uploader.rb'
74
81
  print_notes("carrierwave will use mini_magick by default!")
@@ -77,7 +84,7 @@ private
77
84
  end
78
85
 
79
86
  def setup_kaminari
80
- add_gem("kaminari")
87
+ add_gem { gem "kaminari" }
81
88
  rescue Exception => e
82
89
  raise e
83
90
  end
@@ -87,11 +94,11 @@ private
87
94
  destroy("public/javascripts/ckeditor")
88
95
  ver = ask("==> What version of CKEditor javascript files do you need? [default 3.5.4]")
89
96
  if ver == "3.5.4" || ver.blank?
90
- add_gem("ckeditor","3.5.4")
97
+ add_gem { gem "ckeditor", "3.5.4" }
91
98
  template "ckeditor.rb", "config/initializers/ckeditor.rb"
92
99
  extract("setup/templates/ckeditor.tar.gz","public/javascripts","ckeditor")
93
100
  else
94
- add_gem("ckeditor",ver)
101
+ add_gem { gem "ckeditor", ver }
95
102
  generate("ckeditor:base --version=#{ver}")
96
103
  end
97
104
  rescue Exception => e
@@ -99,35 +106,38 @@ private
99
106
  end
100
107
 
101
108
  def setup_test
102
- gemfile = File.expand_path(destination_path("Gemfile"), __FILE__)
103
-
104
- group_gems = "\ngroup :development, :test do"
105
- group_gems << "\n gem 'rspec-rails'"
106
- group_gems << "\n gem 'capybara'"
107
- group_gems << "\n gem 'factory_girl_rails'"
108
- group_gems << "\n gem 'faker'"
109
- group_gems << "\n gem 'database_cleaner'"
110
- group_gems << "\n gem 'escape_utils'"
111
- group_gems << "\n gem 'guard-rspec'"
112
- group_gems << "\n if RUBY_PLATFORM =~ /darwin/i"
113
- group_gems << "\n gem 'rb-fsevent', :require => false"
114
- group_gems << "\n gem 'growl'"
115
- group_gems << "\n end"
116
- group_gems << "\nend\n"
109
+ # remove the existing install (if any)
110
+ destroy("Guardfile")
117
111
 
118
- File.open(gemfile, 'a') { |f| f.write(group_gems) }
119
- refresh_bundle
112
+ add_gem do
113
+ gem 'rspec-rails', :group => [:development, :test]
114
+ gem 'capybara', :group => [:development, :test]
115
+ gem 'factory_girl_rails', :group => [:development, :test]
116
+ gem 'faker', :group => [:development, :test]
117
+ gem 'database_cleaner', :group => [:development, :test]
118
+ gem 'escape_utils', :group => [:development, :test]
119
+ gem 'guard-rspec', :group => [:development, :test]
120
+ if RUBY_PLATFORM =~ /darwin/i
121
+ gem 'rb-fsevent', :group => [:development, :test], :require => false
122
+ gem 'growl', :group => [:development, :test]
123
+ end
124
+ end
120
125
 
121
126
  copy_file 'escape_utils.rb', 'config/initializers/escape_utils.rb'
127
+ destroy("spec")
122
128
  generate("rspec:install")
123
129
  remove_file 'spec/spec_helper.rb'
124
- template 'spec_helper.rb', 'spec/spec_helper.rb'
130
+ template 'spec_helper.rb', 'spec/spec_helper.rb', :force => true
125
131
  `guard init rspec`
126
132
 
127
133
  asking "Would you like to install Cucumber?" do
128
- add_gem("cucumber-rails", :group => [:development, :test])
129
- add_gem("guard-cucumber", :group => [:development, :test])
134
+ destroy("features")
135
+ add_gem do
136
+ gem "cucumber-rails", :group => [:development, :test]
137
+ gem "guard-cucumber", :group => [:development, :test]
138
+ end
130
139
  generate("cucumber:install", "--rspec", "--capybara")
140
+ template 'env.rb', 'features/support/env.rb', :force => true
131
141
  `guard init cucumber`
132
142
  end
133
143
 
@@ -0,0 +1,50 @@
1
+ # IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
2
+ # It is recommended to regenerate this file in the future when you upgrade to a
3
+ # newer version of cucumber-rails. Consider adding your own code to a new file
4
+ # instead of editing this one. Cucumber will automatically load all features/**/*.rb
5
+ # files.
6
+
7
+ require 'cucumber/rails'
8
+
9
+ # Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
10
+ # order to ease the transition to Capybara we set the default here. If you'd
11
+ # prefer to use XPath just remove this line and adjust any selectors in your
12
+ # steps to use the XPath syntax.
13
+ Capybara.default_selector = :css
14
+
15
+ # By default, any exception happening in your Rails application will bubble up
16
+ # to Cucumber so that your scenario will fail. This is a different from how
17
+ # your application behaves in the production environment, where an error page will
18
+ # be rendered instead.
19
+ #
20
+ # Sometimes we want to override this default behaviour and allow Rails to rescue
21
+ # exceptions and display an error page (just like when the app is running in production).
22
+ # Typical scenarios where you want to do this is when you test your error pages.
23
+ # There are two ways to allow Rails to rescue exceptions:
24
+ #
25
+ # 1) Tag your scenario (or feature) with @allow-rescue
26
+ #
27
+ # 2) Set the value below to true. Beware that doing this globally is not
28
+ # recommended as it will mask a lot of errors for you!
29
+ #
30
+ ActionController::Base.allow_rescue = false
31
+
32
+ # Remove/comment out the lines below if your app doesn't have a database.
33
+ # For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
34
+ begin
35
+ DatabaseCleaner.strategy = <%= using_mongoid? ? ':truncation' : ':transaction' %>
36
+ rescue NameError
37
+ raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
38
+ end
39
+
40
+ # You may also want to configure DatabaseCleaner to use different strategies for certain features and scenarios.
41
+ # See the DatabaseCleaner documentation for details. Example:
42
+ #
43
+ # Before('@no-txn,@selenium,@culerity,@celerity,@javascript') do
44
+ # DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
45
+ # end
46
+ #
47
+ # Before('~@no-txn', '~@selenium', '~@culerity', '~@celerity', '~@javascript') do
48
+ # DatabaseCleaner.strategy = :transaction
49
+ # end
50
+ #
@@ -9,11 +9,11 @@ module Fetty
9
9
  def generate_views
10
10
  case arg
11
11
  when "layout"
12
- generate_layout
12
+ generate_layout
13
13
  when "erb:to:haml"
14
14
  erb_to_haml
15
15
  when "haml:to:erb"
16
- haml_to_erb
16
+ haml_to_erb
17
17
  end
18
18
  rescue Exception => e
19
19
  print_notes(e.message,"error",:red)
@@ -35,13 +35,49 @@ private
35
35
  end
36
36
 
37
37
  def erb_to_haml
38
- print_notes("It will convert your *.html.erb files in 'app/views' to *.html.haml")
38
+ asking "Are you sure want to convert all your views from ERB to HAML ?" do
39
+ # => Cycles through the views folder and searches for erb files
40
+ # prepare_convert_gems
41
+ # Dir.glob("app/views/**/*.erb").each do |file|
42
+ # puts "Convert ERB: #{file}"
43
+ # unless file_exists? file.gsub(/erb$/, "haml")
44
+ # `html2haml #{file} | cat > #{file.gsub(/erb$/, "haml")}`
45
+ # File.delete(file)
46
+ # end
47
+ # end
48
+ end
39
49
  rescue Exception => e
40
50
  raise e
41
51
  end
42
52
 
43
53
  def haml_to_erb
44
- print_notes("It will convert your *.html.haml files in 'app/views' to *.html.erb")
54
+ asking "Are you sure want to convert all your views from HAML to ERB ? [yes]" do
55
+ # => Cycles through the views folder and searches for haml files
56
+ end
57
+ rescue Exception => e
58
+ raise e
59
+ end
60
+
61
+ def prepare_convert_gems
62
+ # install_gem "haml-rails" unless check_installed_gem? "haml-rails"
63
+ # install_gem "hpricot" unless check_installed_gem? "hpricot"
64
+ # install_gem "ruby_parser" unless check_installed_gem? "ruby_parser"
65
+ rescue Exception => e
66
+ raise e
67
+ end
68
+
69
+ def convert_views(in_type,out_type)
70
+ # Dir["app/views/**/*.#{in_type}"].each do |file_name|
71
+ # puts "Convert #{first_type.capitalize}: #{file_name}"
72
+ # out_file_name = file_name.gsub(/#{first_type}$/, second_type)
73
+ # unless file_exists? out_file_name
74
+ # erb_string = File.open(file_name).read
75
+ # haml_string = Haml::HTML.new(erb_string, :erb => true).render
76
+ # f = File.new(haml_file_name, "w")
77
+ # f.write(haml_string)
78
+ # File.delete(file_name)
79
+ # end
80
+ # end
45
81
  rescue Exception => e
46
82
  raise e
47
83
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fetty-generators
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-04 00:00:00.000000000Z
12
+ date: 2011-08-13 00:00:00.000000000Z
13
13
  dependencies: []
14
- description: Simple generator to start your Rails project
14
+ description: This generators provide you to easily setup your Rails 3 application,
15
+ create authentication, messages, admin style scaffolding and many more
15
16
  email:
16
17
  - fajri82@gmail.com
17
18
  executables: []
@@ -105,6 +106,7 @@ files:
105
106
  - lib/generators/fetty/setup/templates/ability.rb
106
107
  - lib/generators/fetty/setup/templates/ckeditor.rb
107
108
  - lib/generators/fetty/setup/templates/ckeditor.tar.gz
109
+ - lib/generators/fetty/setup/templates/env.rb
108
110
  - lib/generators/fetty/setup/templates/escape_utils.rb
109
111
  - lib/generators/fetty/setup/templates/file_uploader.rb
110
112
  - lib/generators/fetty/setup/templates/image_uploader.rb
@@ -148,5 +150,5 @@ rubyforge_project: fetty-generators
148
150
  rubygems_version: 1.8.5
149
151
  signing_key:
150
152
  specification_version: 3
151
- summary: Simple generator to start your Rails project
153
+ summary: Simple generators to start your Rails project
152
154
  test_files: []