rails_assist 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +17 -0
  4. data/LICENSE.txt +20 -0
  5. data/README.markdown +204 -0
  6. data/README.rdoc +19 -0
  7. data/Rakefile +49 -0
  8. data/VERSION +1 -0
  9. data/lib/rails_assist.rb +43 -0
  10. data/lib/rails_assist/app.rb +13 -0
  11. data/lib/rails_assist/artifact.rb +34 -0
  12. data/lib/rails_assist/artifact/directory.rb +66 -0
  13. data/lib/rails_assist/artifact/files.rb +95 -0
  14. data/lib/rails_assist/artifact/path.rb +16 -0
  15. data/lib/rails_assist/directory.rb +64 -0
  16. data/lib/rails_assist/directory/app.rb +21 -0
  17. data/lib/rails_assist/directory/container.rb +24 -0
  18. data/lib/rails_assist/directory/root.rb +24 -0
  19. data/lib/rails_assist/file.rb +91 -0
  20. data/lib/rails_assist/file/application.rb +15 -0
  21. data/lib/rails_assist/file/environment.rb +16 -0
  22. data/lib/rails_assist/file/gem_file.rb +32 -0
  23. data/lib/rails_assist/file/routes_file.rb +13 -0
  24. data/lib/rails_assist/file/special.rb +78 -0
  25. data/lib/rails_assist/files.rb +153 -0
  26. data/lib/rails_assist/macro.rb +68 -0
  27. data/lib/rails_assist/namespaces.rb +11 -0
  28. data/lib/rails_assist/rspec.rb +1 -0
  29. data/lib/rails_assist/rspec/configure.rb +7 -0
  30. data/rails_assist.gemspec +221 -0
  31. data/sandbox/test.rb +16 -0
  32. data/sandbox/test.txt +5 -0
  33. data/spec/fixtures.rb +3 -0
  34. data/spec/fixtures/.gitignore +4 -0
  35. data/spec/fixtures/Gemfile +9 -0
  36. data/spec/fixtures/README +256 -0
  37. data/spec/fixtures/Rakefile +7 -0
  38. data/spec/fixtures/app/controllers/application_controller.rb +3 -0
  39. data/spec/fixtures/app/controllers/users_controller.rb +83 -0
  40. data/spec/fixtures/app/helpers/application_helper.rb +2 -0
  41. data/spec/fixtures/app/helpers/users_helper.rb +2 -0
  42. data/spec/fixtures/app/licenses/blogging_license.rb +0 -0
  43. data/spec/fixtures/app/mailers/user_mailer.rb +0 -0
  44. data/spec/fixtures/app/models/user.rb +2 -0
  45. data/spec/fixtures/app/models/user_observer.rb +0 -0
  46. data/spec/fixtures/app/permits/user_permit.rb +0 -0
  47. data/spec/fixtures/app/validators/email_validator.rb +0 -0
  48. data/spec/fixtures/app/views/layouts/application.html.erb +14 -0
  49. data/spec/fixtures/app/views/users/_form.html.erb +21 -0
  50. data/spec/fixtures/app/views/users/edit.html.erb +6 -0
  51. data/spec/fixtures/app/views/users/index.html.erb +23 -0
  52. data/spec/fixtures/app/views/users/new.html.erb +5 -0
  53. data/spec/fixtures/app/views/users/show.html.erb +10 -0
  54. data/spec/fixtures/config.ru +4 -0
  55. data/spec/fixtures/config/application.rb +43 -0
  56. data/spec/fixtures/config/boot.rb +13 -0
  57. data/spec/fixtures/config/database.yml +22 -0
  58. data/spec/fixtures/config/environment.rb +5 -0
  59. data/spec/fixtures/config/environments/development.rb +26 -0
  60. data/spec/fixtures/config/environments/production.rb +49 -0
  61. data/spec/fixtures/config/environments/test.rb +35 -0
  62. data/spec/fixtures/config/initializers/backtrace_silencers.rb +7 -0
  63. data/spec/fixtures/config/initializers/inflections.rb +10 -0
  64. data/spec/fixtures/config/initializers/mime_types.rb +5 -0
  65. data/spec/fixtures/config/initializers/my_init.rb +0 -0
  66. data/spec/fixtures/config/initializers/secret_token.rb +7 -0
  67. data/spec/fixtures/config/initializers/session_store.rb +8 -0
  68. data/spec/fixtures/config/locales/en.yml +5 -0
  69. data/spec/fixtures/config/routes.rb +60 -0
  70. data/spec/fixtures/db/migrate/20100831135208_create_users.rb +13 -0
  71. data/spec/fixtures/db/seeds.rb +7 -0
  72. data/spec/fixtures/lib/tasks/.gitkeep +0 -0
  73. data/spec/fixtures/public/404.html +26 -0
  74. data/spec/fixtures/public/422.html +26 -0
  75. data/spec/fixtures/public/500.html +26 -0
  76. data/spec/fixtures/public/favicon.ico +0 -0
  77. data/spec/fixtures/public/images/rails.png +0 -0
  78. data/spec/fixtures/public/index.html +239 -0
  79. data/spec/fixtures/public/javascripts/application.js +2 -0
  80. data/spec/fixtures/public/javascripts/controls.js +965 -0
  81. data/spec/fixtures/public/javascripts/dragdrop.js +974 -0
  82. data/spec/fixtures/public/javascripts/effects.js +1 -0
  83. data/spec/fixtures/public/javascripts/prototype.js +6001 -0
  84. data/spec/fixtures/public/javascripts/rails.js +175 -0
  85. data/spec/fixtures/public/robots.txt +5 -0
  86. data/spec/fixtures/public/stylesheets/.gitkeep +0 -0
  87. data/spec/fixtures/public/stylesheets/scaffold.css +56 -0
  88. data/spec/fixtures/script/rails +6 -0
  89. data/spec/fixtures/test/fixtures/users.yml +7 -0
  90. data/spec/fixtures/test/functional/users_controller_test.rb +49 -0
  91. data/spec/fixtures/test/performance/browsing_test.rb +9 -0
  92. data/spec/fixtures/test/test_helper.rb +13 -0
  93. data/spec/fixtures/test/unit/helpers/users_helper_test.rb +4 -0
  94. data/spec/fixtures/test/unit/user_test.rb +8 -0
  95. data/spec/fixtures/vendor/plugins/.gitkeep +0 -0
  96. data/spec/load_spec.rb +1 -0
  97. data/spec/rails_assist/app_spec.rb +31 -0
  98. data/spec/rails_assist/artifact/directory_spec.rb +66 -0
  99. data/spec/rails_assist/artifact/files_spec.rb +79 -0
  100. data/spec/rails_assist/artifact/path_spec.rb +23 -0
  101. data/spec/rails_assist/artifact_spec.rb +33 -0
  102. data/spec/rails_assist/directory/app_spec.rb +45 -0
  103. data/spec/rails_assist/directory/container_spec.rb +63 -0
  104. data/spec/rails_assist/directory/root_spec.rb +46 -0
  105. data/spec/rails_assist/directory_spec.rb +64 -0
  106. data/spec/rails_assist/file/application_spec.rb +33 -0
  107. data/spec/rails_assist/file/environment_spec.rb +49 -0
  108. data/spec/rails_assist/file/special_spec.rb +171 -0
  109. data/spec/rails_assist/file_spec.rb +115 -0
  110. data/spec/rails_assist/files_spec.rb +90 -0
  111. data/spec/rails_assist/rspec/have_app_config_spec.rb +0 -0
  112. data/spec/rails_assist_spec.rb +7 -0
  113. data/spec/spec_helper.rb +53 -0
  114. metadata +291 -0
@@ -0,0 +1,95 @@
1
+ require 'sugar-high/regexp'
2
+
3
+ module RailsAssist::Artifact
4
+ module Files
5
+ module Methods
6
+ [:model].each do |name|
7
+ class_eval %{
8
+ def #{name}_filepaths expr=nil
9
+ files = RailsAssist::Files.rails_app_files(:#{name.to_s.pluralize}).grep_it expr
10
+ yield files if block_given?
11
+ files
12
+ end
13
+
14
+ def #{name}_files expr=nil
15
+ files = #{name}_filepaths(expr).to_files
16
+ yield files if block_given?
17
+ files
18
+ end
19
+ }
20
+ end
21
+
22
+ [:initializer, :locale].each do |name|
23
+ class_eval %{
24
+ def #{name}_filepaths expr=nil
25
+ files = RailsAssist::Files.rails_app_files(:#{name.to_s.pluralize}).grep_it expr
26
+ yield files if block_given?
27
+ files
28
+ end
29
+ }
30
+ end
31
+
32
+ # artifact files using xxx_[artifact].rb convention, i.e postfixing with type of artifact
33
+ [:mailer, :observer, :permit, :license, :controller, :helper, :validator].each do |name|
34
+ class_eval %{
35
+ def #{name}_filepaths expr=nil
36
+ files = RailsAssist::Files.rails_app_files(:#{name.to_s.pluralize}, :pattern => '**/*_#{name}.rb').grep_it expr
37
+ yield files if block_given?
38
+ files
39
+ end
40
+
41
+ def #{name}_files expr=nil
42
+ files = #{name}_filepaths(expr).to_files
43
+ yield files if block_given?
44
+ files
45
+ end
46
+ }
47
+ end
48
+
49
+ def view_filepaths *args
50
+ expr, model_name = Helper.get_view_args args
51
+ ext = last_option(args)[:template_language] || 'erb'
52
+ pattern = model_name ? "#{model_name.to_s.pluralize}/*.#{ext}*" : "**/*.#{ext}*"
53
+ files = RailsAssist::Files.rails_app_files(:views, :pattern => pattern).grep_it expr
54
+ yield files if block_given?
55
+ files
56
+ end
57
+
58
+ def view_files *args
59
+ files = view_filepaths(args).to_files
60
+ yield files if block_given?
61
+ files
62
+ end
63
+
64
+
65
+ [:erb, :haml, :slim].each do |name|
66
+ class_eval %{
67
+ def #{name}_view_filepaths *args
68
+ view_files args, :template_language => :#{name}
69
+ end
70
+
71
+ def #{name}_view_files *args
72
+ view_filepaths(args).to_files
73
+ end
74
+ }
75
+ end
76
+ end
77
+ extend Methods
78
+ include Methods
79
+
80
+ module Helper
81
+ def self.get_view_args args
82
+ args = args.flatten
83
+ first_arg = args.first
84
+ case first_arg
85
+ when Regexp
86
+ expr = first_arg
87
+ when String, Symbol
88
+ _model = first_arg
89
+ end
90
+ expr = args[1] if args.size > 1 && args[1].kind_of?(Regexp)
91
+ [expr, _model]
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,16 @@
1
+ module RailsAssist::Artifact
2
+ module Path
3
+ include RailsAssist::Artifact::Directory
4
+
5
+ def artifact_path name, type, options={}
6
+ dir = send :"#{type}_dir", options
7
+ File.join(dir, "#{name}#{type_postfix type}.rb")
8
+ end
9
+
10
+ protected
11
+
12
+ def type_postfix type
13
+ "_#{type}" if ![:model].include?(type)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,64 @@
1
+ require_all File.dirname(__FILE__) + '/directory'
2
+ require 'rails_assist/artifact'
3
+
4
+ module RailsAssist
5
+ module Directory
6
+ class << self
7
+ attr_accessor :rails_root
8
+
9
+ def method_missing(sym, *args, &block)
10
+ RailsAssist::Artifact::Directory.send sym
11
+ end
12
+ end
13
+
14
+ include Root
15
+ extend Root
16
+
17
+ include App
18
+ extend App
19
+
20
+ include Container
21
+ extend Container
22
+
23
+ # dir_for helpers
24
+ # -------------------
25
+
26
+ module Methods
27
+ DIR = RailsAssist::Artifact::Directory
28
+
29
+ # :app, :config, :db, :public, :lib, :log, :doc, :test, :spec
30
+ #
31
+ # app_dir, config_dir ...
32
+ RailsAssist::Directory::Root.root_directories.each do |name|
33
+ class_eval %{
34
+ def #{name}_dir options={}
35
+ ::File.join(RailsAssist::Directory::Root.root_dir(options), '#{name}')
36
+ end
37
+ }
38
+ end
39
+
40
+ def rails_dir_for type, options={}
41
+ raise ArgumentError, '#rails_dir_for takes a dir type argument' if !type
42
+ dir_method = "#{type}_dir"
43
+ return send(dir_method, options) if respond_to?(dir_method)
44
+ DIR.send(dir_method, options) if DIR.respond_to?(dir_method)
45
+ end
46
+
47
+ def app_dir_for type, options={}
48
+ ::File.join(app_dir(options), type.to_s.pluralize)
49
+ end
50
+
51
+ def config_dir_for type, options={}
52
+ ::File.join(config_dir(options), type.to_s.pluralize)
53
+ end
54
+
55
+ def public_dir_for type, options={}
56
+ ::File.join(public_dir(options), type.to_s.pluralize)
57
+ end
58
+ end
59
+
60
+ include Methods
61
+ extend Methods
62
+
63
+ end # Directories
64
+ end
@@ -0,0 +1,21 @@
1
+ module RailsAssist::Directory
2
+ module App
3
+ module Methods
4
+ def app_directories
5
+ RailsAssist::Artifact::app_artifacts.map {|a| a.to_s.pluralize.to_sym}
6
+ end
7
+
8
+ def app_dirpath options={}
9
+ rails_root_dir = RailsAssist::Directory::Root.root_dir(options)
10
+ [rails_root_dir, 'app'].file_join
11
+ end
12
+
13
+ def app_dir options={}
14
+ app_dirpath(options).dir
15
+ end
16
+ end
17
+
18
+ include Methods
19
+ extend Methods
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ module RailsAssist::Directory
2
+ module Container
3
+
4
+ # return list of symbols for all kinds of supported Rails 3 containers
5
+ # (those that have a [container]_dir method)
6
+
7
+ module Methods
8
+ def rails_containers
9
+ app_methods = RailsAssist::Directory.instance_methods
10
+ artifact_methods = RailsAssist::Artifact::Directory.instance_methods
11
+ all_methods = app_methods + artifact_methods
12
+
13
+ all_methods.grep(/_dir$/).map{|dir| dir.to_s.gsub(/^(.*)_dir/, '\1').to_sym }
14
+ end
15
+
16
+ def valid_container? type
17
+ rails_containers.include?(type)
18
+ end
19
+ end
20
+
21
+ include Methods
22
+ extend Methods
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module RailsAssist::Directory
2
+ module Root
3
+ module Methods
4
+ def root_directories
5
+ [:app, :config, :db, :public, :lib, :log, :doc, :test, :spec]
6
+ end
7
+
8
+ def root_dirpath options={}
9
+ raise ArgumentError, "options argument to root_dir must be a hash, was: #{options.inspect}" if options && !options.kind_of?(Hash)
10
+ dir = options[:root_path] if options
11
+ dir ||= RailsAssist::Directory.rails_root || Rails.root
12
+ raise "You must set the Rails app root dir: RailsAssist::App.root_dir = '/my/root/dir'" if !dir
13
+ dir.path
14
+ end
15
+
16
+ def root_dir options={}
17
+ root_dirpath(options).dir
18
+ end
19
+ end
20
+
21
+ include Methods
22
+ extend Methods
23
+ end
24
+ end
@@ -0,0 +1,91 @@
1
+ require_all File.dirname(__FILE__) + '/file'
2
+ # require 'sugar-high/file'
3
+
4
+ module RailsAssist
5
+ module File
6
+ module Methods
7
+ {:initializer => 'rb', :db => 'rb', :migration => 'rb', :locale => 'yml', :javascript => 'js', :stylesheet => 'css', :config => '.rb', :db => '.rb'}.each_pair do |name, ext|
8
+ plural_name = name.to_s.pluralize
9
+ pure_ext = ext.gsub /^\./, ''
10
+ class_eval %{
11
+ def #{name}_file? name
12
+ name = name.as_filename
13
+ name = (name =~ /.rb$/) ? name : "\#{name}.#{pure_ext}"
14
+ #{name}_file(name)
15
+ end
16
+ alias_method :has_#{name}_file?, :#{name}_file?
17
+
18
+ def #{name}_files? *names
19
+ names.to_filenames.each do |name|
20
+ return false if !#{name}_file?(name)
21
+ end
22
+ true
23
+ end
24
+
25
+ def #{name}_filepath name
26
+ name = name.as_filename
27
+ name = (name =~ /.rb$/) ? name : "\#{name}.#{pure_ext}"
28
+ [RailsAssist::Artifact::Directory.#{name}_dir, name].file_join
29
+ end
30
+
31
+ def #{name}_file name
32
+ name = name.as_filename
33
+ #{name}_filepath(name).new_file
34
+ end
35
+
36
+ def create_#{name} name, &block
37
+ name = name.as_filename
38
+
39
+ # create dir for artifact if it doesn't exist
40
+ RailsAssist::App.create_empty_tmp :#{name} if !RailsAssist::Artifact::Directory.#{name}_dir
41
+
42
+ #{name}_file(name).overwrite do
43
+ yield
44
+ end
45
+ end
46
+
47
+ def read_#{name} name
48
+ name = name.as_filename
49
+ #{name}_file(name).read_content
50
+ end
51
+ alias_method :read_#{name}_file, :read_#{name}
52
+
53
+ def replace_#{name}_content name, args = {}, &block
54
+ name = name.as_filename
55
+ if !(args.kind_of?(Hash) && args[:where])
56
+ raise ArgumentError, "#replace_#{name}_content Must take a hash argument with a :where option that is the content to be matched and replaced"
57
+ end
58
+
59
+ replace_content = block ? yield : args[:with]
60
+ if !replace_content
61
+ raise ArgumentError, "#replace_#{name}_content Must take a block or a :with hash option that is the content to replace with"
62
+ end
63
+
64
+ #{name}_file(name).replace_content :where => args[:where], :with => replace_content
65
+ end
66
+
67
+ def remove_all_#{plural_name}
68
+ return if !(RailsAssist::Artifact::Directory.#{name}_dir.path.directory?)
69
+ #{name}_files.delete_all!
70
+ end
71
+
72
+ def remove_#{plural_name} *names
73
+ return remove_all_#{plural_name} if names.empty?
74
+ names.to_filenames.each do |name|
75
+ name = (name =~ /.rb$/) ? name : "\#{name}.#{pure_ext}"
76
+ #{name}_file(name).delete!
77
+ end
78
+ end
79
+ alias_method :remove_#{name}, :remove_#{plural_name}
80
+ }
81
+ end
82
+ end
83
+
84
+ include Special
85
+ extend Special
86
+
87
+ include Methods
88
+ extend Methods
89
+ end
90
+ end
91
+
@@ -0,0 +1,15 @@
1
+ require 'sugar-high/file'
2
+ require 'sugar-high/file_mutate'
3
+
4
+ module RailsAssist::File
5
+ module Application
6
+ module Methods
7
+ def insert_application_config statement=nil, &block
8
+ statement = block ? yield : "config.#{statement}"
9
+ application_file.insert statement, :after => 'Rails::Application'
10
+ end
11
+ end
12
+ include Methods
13
+ extend Methods
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ module RailsAssist::File
2
+ module Environment
3
+ def insert_before_application_init content=nil, &block
4
+ insert_application_init :before, content, &block
5
+ end
6
+
7
+ def insert_after_application_init content=nil, &block
8
+ insert_application_init :after, content, &block
9
+ end
10
+
11
+ def insert_application_init place, statement=nil, &block
12
+ statement = block ? yield : statement
13
+ environment_file.insert statement, place => /\w+#{Regexp.escape('::Application.initialize!')}/
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,32 @@
1
+ module RailsAssist::File
2
+ module Gemfile
3
+ def has_gem? name
4
+ read_gem_file =~ /gem\s+('|")#{name}\1/
5
+ end
6
+
7
+ def has_gem_version? name, version = nil
8
+ read_gem_file =~ /gem\s+('|")#{name}\1,\s*('|")#{version}\2/
9
+ end
10
+
11
+ def clean_gemfile
12
+ clean_gemfile_content = read_gem_file.gsub(/('|"|end)\s*gem/m, "\\1\ngem")
13
+ read_gem_file.overwrite clean_gemfile_content
14
+ end
15
+
16
+ def gem_filepath
17
+ [RailsAssist::Directory.rails_root.path, 'Gemfile'].file_join
18
+ end
19
+
20
+ def gem_file
21
+ raise "No Gemfile found at #{gem_filepath}" if !File.exist? gem_filepath
22
+ gem_filepath.new_file
23
+ end
24
+
25
+ # def with_gem_file command = nil
26
+ # return send :"#{command}_gem_file" if command.valid_file_command?
27
+ #
28
+ # raise "No Gemfile found at #{gem_filepath}" if gem_filepath.exists?
29
+ # gem_filepath.new_file
30
+ # end
31
+ end
32
+ end
@@ -0,0 +1,13 @@
1
+ module RailsAssist::File
2
+ module Routes
3
+ def insert_into_routes route_stmt=nil, &block
4
+ statement = block ? yield : route_stmt
5
+ routes_file.insert statement, :after => 'routes.draw do'
6
+ end
7
+
8
+ def insert_last_in_routes route_stmt=nil, &block
9
+ statement = block ? yield : route_stmt
10
+ routes_file.insert statement, :before_last => 'end'
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,78 @@
1
+ require_all File.dirname(__FILE__)
2
+
3
+ module RailsAssist::File
4
+ module Special
5
+ module Methods
6
+ # application_file, environment_file
7
+ [:application, :environment, :routes, :boot].each do |name|
8
+ class_eval %{
9
+ def #{name}_filepath
10
+ [RailsAssist::Directory.config_dir.path, '#{name}.rb'].file_join
11
+ end
12
+
13
+ def #{name}_file
14
+ #{name}_filepath.file
15
+ end
16
+ }
17
+ end
18
+
19
+ def database_filepath
20
+ [RailsAssist::Directory.config_dir.path, 'database.yml'].file_join
21
+ end
22
+
23
+ def database_file
24
+ raise "No Database file found at #{database_filepath}" if File.file? database_filepath
25
+ File.new(database_filepath)
26
+ end
27
+
28
+ def seed_filepath
29
+ [RailsAssist::Directory.db_dir.path, 'seeds.rb'].file_join
30
+ end
31
+
32
+ def seed_file
33
+ raise "No Seed file found at #{seed_filepath}" if !File.exist? seed_filepath
34
+ seed_filepath.new_file
35
+ end
36
+
37
+ # read_application_file
38
+ # append_to_application_file
39
+ [:application, :environment, :seed, :gem, :routes, :boot, :database].each do |name|
40
+ class_eval %{
41
+ def read_#{name}_file
42
+ #{name}_file.read_content
43
+ end
44
+
45
+ def remove_#{name}_file
46
+ #{name}_file.delete!
47
+ end
48
+
49
+ def append_to_#{name}_file content=nil, &block
50
+ #{name}_file.append content, &block
51
+ end
52
+
53
+ def replace_in_#{name}_file options = {}, &block
54
+ #{name}_file.replace_content options, &block
55
+ end
56
+
57
+ def remove_from_#{name}_file content=nil, &block
58
+ #{name}_file.remove_content content, &block
59
+ end
60
+ }
61
+ end
62
+ end
63
+
64
+ include Methods
65
+ extend Methods
66
+
67
+ include RailsAssist::File::Application
68
+ include RailsAssist::File::Environment
69
+ include RailsAssist::File::Gemfile
70
+ include RailsAssist::File::Routes
71
+
72
+ extend RailsAssist::File::Application
73
+ extend RailsAssist::File::Environment
74
+ extend RailsAssist::File::Gemfile
75
+ extend RailsAssist::File::Routes
76
+
77
+ end
78
+ end