merb-admin 0.8.1 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. data/.gitignore +29 -27
  2. data/Gemfile +24 -22
  3. data/Gemfile.lock +86 -0
  4. data/LICENSE +20 -20
  5. data/README.rdoc +54 -58
  6. data/Rakefile +11 -44
  7. data/app/controllers/application.rb +6 -6
  8. data/app/controllers/main.rb +191 -191
  9. data/app/helpers/application_helper.rb +64 -64
  10. data/app/helpers/main_helper.rb +167 -167
  11. data/app/models/.gitkeep +0 -0
  12. data/app/views/layout/_message.html.erb +10 -10
  13. data/app/views/layout/dashboard.html.erb +34 -34
  14. data/app/views/layout/form.html.erb +48 -48
  15. data/app/views/layout/list.html.erb +42 -42
  16. data/app/views/main/_belongs_to.html.erb +29 -29
  17. data/app/views/main/_big_decimal.html.erb +12 -12
  18. data/app/views/main/_boolean.html.erb +7 -7
  19. data/app/views/main/_date.html.erb +12 -12
  20. data/app/views/main/_datetime.html.erb +12 -12
  21. data/app/views/main/_float.html.erb +12 -12
  22. data/app/views/main/_has_many.html.erb +16 -16
  23. data/app/views/main/_has_one.html.erb +30 -30
  24. data/app/views/main/_integer.html.erb +12 -12
  25. data/app/views/main/_properties.html.erb +18 -18
  26. data/app/views/main/_string.html.erb +15 -15
  27. data/app/views/main/_text.html.erb +11 -11
  28. data/app/views/main/_time.html.erb +12 -12
  29. data/app/views/main/_timestamp.html.erb +12 -12
  30. data/app/views/main/delete.html.erb +28 -28
  31. data/app/views/main/edit.html.erb +19 -19
  32. data/app/views/main/index.html.erb +22 -22
  33. data/app/views/main/list.html.erb +93 -93
  34. data/app/views/main/new.html.erb +16 -16
  35. data/config/init.rb +30 -0
  36. data/config/router.rb +4 -0
  37. data/lib/abstract_model.rb +84 -86
  38. data/lib/active_record_support.rb +147 -147
  39. data/lib/datamapper_support.rb +139 -140
  40. data/lib/generic_support.rb +13 -13
  41. data/lib/merb-admin.rb +99 -99
  42. data/lib/merb-admin/merbtasks.rb +103 -103
  43. data/lib/merb-admin/slicetasks.rb +174 -174
  44. data/lib/merb-admin/spectasks.rb +55 -55
  45. data/lib/merb-admin/version.rb +3 -0
  46. data/lib/sequel_support.rb +275 -275
  47. data/merb-admin.gemspec +42 -232
  48. data/public/javascripts/CollapsedFieldsets.js +85 -85
  49. data/public/javascripts/DateTimeShortcuts.js +255 -255
  50. data/public/javascripts/RelatedObjectLookups.js +96 -96
  51. data/public/javascripts/SelectBox.js +111 -111
  52. data/public/javascripts/SelectFilter2.js +113 -113
  53. data/public/javascripts/actions.js +39 -39
  54. data/public/javascripts/calendar.js +143 -143
  55. data/public/javascripts/core.js +176 -176
  56. data/public/javascripts/dateparse.js +233 -233
  57. data/public/javascripts/getElementsBySelector.js +167 -167
  58. data/public/javascripts/i18n.js +33 -33
  59. data/public/javascripts/ordering.js +137 -137
  60. data/public/javascripts/timeparse.js +94 -94
  61. data/public/javascripts/urlify.js +140 -140
  62. data/public/stylesheets/base.css +746 -746
  63. data/public/stylesheets/changelists.css +269 -269
  64. data/public/stylesheets/dashboard.css +24 -24
  65. data/public/stylesheets/forms.css +327 -327
  66. data/public/stylesheets/global.css +142 -142
  67. data/public/stylesheets/ie.css +50 -50
  68. data/public/stylesheets/layout.css +29 -29
  69. data/public/stylesheets/login.css +54 -54
  70. data/public/stylesheets/master.css +1 -1
  71. data/public/stylesheets/patch-iewin.css +7 -7
  72. data/public/stylesheets/rtl.css +206 -206
  73. data/public/stylesheets/widgets.css +506 -506
  74. data/screenshots/create.png +0 -0
  75. data/screenshots/delete.png +0 -0
  76. data/screenshots/edit.png +0 -0
  77. data/screenshots/index.png +0 -0
  78. data/screenshots/list.png +0 -0
  79. data/screenshots/new.png +0 -0
  80. data/spec/controllers/main_spec.rb +25 -25
  81. data/spec/migrations/activerecord/001_create_divisions_migration.rb +13 -13
  82. data/spec/migrations/activerecord/002_create_drafts_migration.rb +19 -19
  83. data/spec/migrations/activerecord/003_create_leagues_migration.rb +12 -12
  84. data/spec/migrations/activerecord/004_create_players_migration.rb +19 -20
  85. data/spec/migrations/activerecord/005_create_teams_migration.rb +22 -22
  86. data/spec/migrations/sequel/001_create_divisions_migration.rb +15 -15
  87. data/spec/migrations/sequel/002_create_drafts_migration.rb +21 -21
  88. data/spec/migrations/sequel/003_create_leagues_migration.rb +14 -14
  89. data/spec/migrations/sequel/004_create_players_migration.rb +21 -22
  90. data/spec/migrations/sequel/005_create_teams_migration.rb +24 -24
  91. data/spec/models/activerecord/division.rb +7 -7
  92. data/spec/models/activerecord/draft.rb +11 -11
  93. data/spec/models/activerecord/league.rb +6 -6
  94. data/spec/models/activerecord/player.rb +8 -8
  95. data/spec/models/activerecord/team.rb +13 -13
  96. data/spec/models/datamapper/division.rb +12 -12
  97. data/spec/models/datamapper/draft.rb +18 -18
  98. data/spec/models/datamapper/league.rb +11 -11
  99. data/spec/models/datamapper/player.rb +20 -21
  100. data/spec/models/datamapper/team.rb +22 -22
  101. data/spec/models/sequel/division.rb +15 -15
  102. data/spec/models/sequel/draft.rb +19 -19
  103. data/spec/models/sequel/league.rb +14 -14
  104. data/spec/models/sequel/player.rb +18 -18
  105. data/spec/models/sequel/team.rb +21 -21
  106. data/spec/requests/main_spec.rb +763 -763
  107. data/spec/spec_helper.rb +113 -112
  108. metadata +247 -41
  109. data/VERSION +0 -1
@@ -1,103 +1,103 @@
1
- namespace :slices do
2
- namespace :"merb-admin" do
3
-
4
- desc "Install MerbAdmin"
5
- task :install => [:preflight, :setup_directories, :copy_assets, :migrate]
6
-
7
- desc "Test for any dependencies"
8
- task :preflight do # see slicetasks.rb
9
- end
10
-
11
- desc "Setup directories"
12
- task :setup_directories do
13
- puts "Creating directories for host application"
14
- MerbAdmin.mirrored_components.each do |type|
15
- if File.directory?(MerbAdmin.dir_for(type))
16
- if !File.directory?(dst_path = MerbAdmin.app_dir_for(type))
17
- relative_path = dst_path.relative_path_from(Merb.root)
18
- puts "- creating directory :#{type} #{File.basename(Merb.root) / relative_path}"
19
- mkdir_p(dst_path)
20
- end
21
- end
22
- end
23
- end
24
-
25
- # desc "Copy stub files to host application"
26
- # task :stubs do
27
- # puts "Copying stubs for MerbAdmin - resolves any collisions"
28
- # copied, preserved = MerbAdmin.mirror_stubs!
29
- # puts "- no files to copy" if copied.empty? && preserved.empty?
30
- # copied.each { |f| puts "- copied #{f}" }
31
- # preserved.each { |f| puts "! preserved override as #{f}" }
32
- # end
33
-
34
- # desc "Copy stub files and views to host application"
35
- # task :patch => [ "stubs", "freeze:views" ]
36
-
37
- desc "Copy public assets to host application"
38
- task :copy_assets do
39
- puts "Copying assets for MerbAdmin - resolves any collisions"
40
- copied, preserved = MerbAdmin.mirror_public!
41
- puts "- no files to copy" if copied.empty? && preserved.empty?
42
- copied.each { |f| puts "- copied #{f}" }
43
- preserved.each { |f| puts "! preserved override as #{f}" }
44
- end
45
-
46
- desc "Migrate the database"
47
- task :migrate do # see slicetasks.rb
48
- end
49
-
50
- desc "Freeze MerbAdmin into your app (only merb-admin/app)"
51
- task :freeze => [ "freeze:app" ]
52
-
53
- namespace :freeze do
54
-
55
- # desc "Freezes MerbAdmin by installing the gem into application/gems"
56
- # task :gem do
57
- # ENV["GEM"] ||= "merb-admin"
58
- # Rake::Task['slices:install_as_gem'].invoke
59
- # end
60
-
61
- desc "Freezes MerbAdmin by copying all files from merb-admin/app to your application"
62
- task :app do
63
- puts "Copying all merb-admin/app files to your application - resolves any collisions"
64
- copied, preserved = MerbAdmin.mirror_app!
65
- puts "- no files to copy" if copied.empty? && preserved.empty?
66
- copied.each { |f| puts "- copied #{f}" }
67
- preserved.each { |f| puts "! preserved override as #{f}" }
68
- end
69
-
70
- desc "Freeze all views into your application for easy modification"
71
- task :views do
72
- puts "Copying all view templates to your application - resolves any collisions"
73
- copied, preserved = MerbAdmin.mirror_files_for :view
74
- puts "- no files to copy" if copied.empty? && preserved.empty?
75
- copied.each { |f| puts "- copied #{f}" }
76
- preserved.each { |f| puts "! preserved override as #{f}" }
77
- end
78
-
79
- desc "Freeze all models into your application for easy modification"
80
- task :models do
81
- puts "Copying all models to your application - resolves any collisions"
82
- copied, preserved = MerbAdmin.mirror_files_for :model
83
- puts "- no files to copy" if copied.empty? && preserved.empty?
84
- copied.each { |f| puts "- copied #{f}" }
85
- preserved.each { |f| puts "! preserved override as #{f}" }
86
- end
87
-
88
- desc "Freezes MerbAdmin as a gem and copies over merb-admin/app"
89
- task :app_with_gem => [:gem, :app]
90
-
91
- desc "Freezes MerbAdmin by unpacking all files into your application"
92
- task :unpack do
93
- puts "Unpacking MerbAdmin files to your application - resolves any collisions"
94
- copied, preserved = MerbAdmin.unpack_slice!
95
- puts "- no files to copy" if copied.empty? && preserved.empty?
96
- copied.each { |f| puts "- copied #{f}" }
97
- preserved.each { |f| puts "! preserved override as #{f}" }
98
- end
99
-
100
- end
101
-
102
- end
103
- end
1
+ namespace :slices do
2
+ namespace :"merb-admin" do
3
+
4
+ desc "Install MerbAdmin"
5
+ task :install => [:preflight, :setup_directories, :copy_assets, :migrate]
6
+
7
+ desc "Test for any dependencies"
8
+ task :preflight do # see slicetasks.rb
9
+ end
10
+
11
+ desc "Setup directories"
12
+ task :setup_directories do
13
+ puts "Creating directories for host application"
14
+ MerbAdmin.mirrored_components.each do |type|
15
+ if File.directory?(MerbAdmin.dir_for(type))
16
+ if !File.directory?(dst_path = MerbAdmin.app_dir_for(type))
17
+ relative_path = dst_path.relative_path_from(Merb.root)
18
+ puts "- creating directory :#{type} #{File.basename(Merb.root) / relative_path}"
19
+ mkdir_p(dst_path)
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ # desc "Copy stub files to host application"
26
+ # task :stubs do
27
+ # puts "Copying stubs for MerbAdmin - resolves any collisions"
28
+ # copied, preserved = MerbAdmin.mirror_stubs!
29
+ # puts "- no files to copy" if copied.empty? && preserved.empty?
30
+ # copied.each { |f| puts "- copied #{f}" }
31
+ # preserved.each { |f| puts "! preserved override as #{f}" }
32
+ # end
33
+
34
+ # desc "Copy stub files and views to host application"
35
+ # task :patch => [ "stubs", "freeze:views" ]
36
+
37
+ desc "Copy public assets to host application"
38
+ task :copy_assets do
39
+ puts "Copying assets for MerbAdmin - resolves any collisions"
40
+ copied, preserved = MerbAdmin.mirror_public!
41
+ puts "- no files to copy" if copied.empty? && preserved.empty?
42
+ copied.each { |f| puts "- copied #{f}" }
43
+ preserved.each { |f| puts "! preserved override as #{f}" }
44
+ end
45
+
46
+ desc "Migrate the database"
47
+ task :migrate do # see slicetasks.rb
48
+ end
49
+
50
+ desc "Freeze MerbAdmin into your app (only merb-admin/app)"
51
+ task :freeze => [ "freeze:app" ]
52
+
53
+ namespace :freeze do
54
+
55
+ # desc "Freezes MerbAdmin by installing the gem into application/gems"
56
+ # task :gem do
57
+ # ENV["GEM"] ||= "merb-admin"
58
+ # Rake::Task['slices:install_as_gem'].invoke
59
+ # end
60
+
61
+ desc "Freezes MerbAdmin by copying all files from merb-admin/app to your application"
62
+ task :app do
63
+ puts "Copying all merb-admin/app files to your application - resolves any collisions"
64
+ copied, preserved = MerbAdmin.mirror_app!
65
+ puts "- no files to copy" if copied.empty? && preserved.empty?
66
+ copied.each { |f| puts "- copied #{f}" }
67
+ preserved.each { |f| puts "! preserved override as #{f}" }
68
+ end
69
+
70
+ desc "Freeze all views into your application for easy modification"
71
+ task :views do
72
+ puts "Copying all view templates to your application - resolves any collisions"
73
+ copied, preserved = MerbAdmin.mirror_files_for :view
74
+ puts "- no files to copy" if copied.empty? && preserved.empty?
75
+ copied.each { |f| puts "- copied #{f}" }
76
+ preserved.each { |f| puts "! preserved override as #{f}" }
77
+ end
78
+
79
+ desc "Freeze all models into your application for easy modification"
80
+ task :models do
81
+ puts "Copying all models to your application - resolves any collisions"
82
+ copied, preserved = MerbAdmin.mirror_files_for :model
83
+ puts "- no files to copy" if copied.empty? && preserved.empty?
84
+ copied.each { |f| puts "- copied #{f}" }
85
+ preserved.each { |f| puts "! preserved override as #{f}" }
86
+ end
87
+
88
+ desc "Freezes MerbAdmin as a gem and copies over merb-admin/app"
89
+ task :app_with_gem => [:gem, :app]
90
+
91
+ desc "Freezes MerbAdmin by unpacking all files into your application"
92
+ task :unpack do
93
+ puts "Unpacking MerbAdmin files to your application - resolves any collisions"
94
+ copied, preserved = MerbAdmin.unpack_slice!
95
+ puts "- no files to copy" if copied.empty? && preserved.empty?
96
+ copied.each { |f| puts "- copied #{f}" }
97
+ preserved.each { |f| puts "! preserved override as #{f}" }
98
+ end
99
+
100
+ end
101
+
102
+ end
103
+ end
@@ -1,174 +1,174 @@
1
- require 'abstract_model'
2
-
3
- namespace :slices do
4
- namespace :"merb-admin" do
5
-
6
- # # Uncomment the following lines and edit the pre defined tasks
7
- #
8
- # # implement this to test for structural/code dependencies
9
- # # like certain directories or availability of other files
10
- # desc "Test for any dependencies"
11
- # task :preflight do
12
- # end
13
- #
14
- # # implement this to perform any database related setup steps
15
- # desc "Migrate the database"
16
- # task :migrate do
17
- # end
18
-
19
- desc "Copies sample models, copies and runs sample migrations, and loads sample data into your app"
20
- task :activerecord => ["activerecord:copy_sample_models", "activerecord:copy_sample_migrations", "activerecord:migrate", "load_sample_data"]
21
- namespace :activerecord do
22
- desc "Copies sample models into your app"
23
- task :copy_sample_models do
24
- copy_models(:activerecord)
25
- end
26
-
27
- desc "Copies sample migrations into your app"
28
- task :copy_sample_migrations do
29
- copy_migrations(:activerecord)
30
- end
31
-
32
- desc "Migrate the database to the latest version"
33
- task :migrate do
34
- Rake::Task["db:migrate"].reenable
35
- Rake::Task["db:migrate"].invoke
36
- end
37
- end
38
-
39
- desc "Copies sample models, runs sample migrations, and loads sample data into your app"
40
- task :datamapper => ["datamapper:copy_sample_models", "datamapper:migrate", "load_sample_data"]
41
- namespace :datamapper do
42
- desc "Copies sample models into your app"
43
- task :copy_sample_models do
44
- copy_models(:datamapper)
45
- end
46
-
47
- desc "Perform non destructive automigration"
48
- task :migrate do
49
- Rake::Task["db:automigrate"].reenable
50
- Rake::Task["db:automigrate"].invoke
51
- end
52
- end
53
-
54
- desc "Copies sample models, copies and runs sample migrations, and loads sample data"
55
- task :sequel => ["sequel:copy_sample_models", "sequel:copy_sample_migrations", "sequel:migrate", "load_sample_data"]
56
- namespace :sequel do
57
- desc "Copies sample models into your app"
58
- task :copy_sample_models do
59
- copy_models(:sequel)
60
- end
61
-
62
- desc "Copies sample migrations into your app"
63
- task :copy_sample_migrations do
64
- copy_migrations(:sequel)
65
- end
66
-
67
- desc "Perform migration using migrations in schema/migrations"
68
- task :migrate do
69
- require 'sequel/extensions/migration'
70
- Rake::Task["sequel:db:migrate"].reenable
71
- Rake::Task["sequel:db:migrate"].invoke
72
- end
73
- end
74
-
75
- desc "Loads sample data into your app"
76
- task :load_sample_data do
77
- load_data
78
- end
79
-
80
- end
81
- end
82
-
83
- private
84
-
85
- def load_data
86
- begin
87
- require "mlb"
88
- rescue LoadError => e
89
- puts "LoadError: #{e}"
90
- puts "gem install mlb -s http://gemcutter.org"
91
- return
92
- end
93
-
94
- require_models
95
-
96
- puts "Loading current MLB leagues, divisions, teams, and players"
97
- MLB::Team.all.each do |mlb_team|
98
- unless league = MerbAdmin::AbstractModel.new("League").first(:conditions => ["name = ?", mlb_team.league])
99
- league = MerbAdmin::AbstractModel.new("League").create(:name => mlb_team.league)
100
- end
101
- unless division = MerbAdmin::AbstractModel.new("Division").first(:conditions => ["name = ?", mlb_team.division])
102
- division = MerbAdmin::AbstractModel.new("Division").create(:name => mlb_team.division, :league => league)
103
- end
104
- unless team = MerbAdmin::AbstractModel.new("Team").first(:conditions => ["name = ?", mlb_team.name])
105
- team = MerbAdmin::AbstractModel.new("Team").create(:name => mlb_team.name, :logo_url => mlb_team.logo_url, :manager => mlb_team.manager, :ballpark => mlb_team.ballpark, :mascot => mlb_team.mascot, :founded => mlb_team.founded, :wins => mlb_team.wins, :losses => mlb_team.losses, :win_percentage => ("%.3f" % (mlb_team.wins.to_f / (mlb_team.wins + mlb_team.losses))).to_f, :division => division, :league => league)
106
- end
107
- mlb_team.players.reject{|player| player.number.nil?}.each do |player|
108
- MerbAdmin::AbstractModel.new("Player").create(:name => player.name, :number => player.number, :position => player.position, :team => team)
109
- end
110
- end
111
- end
112
-
113
- def copy_models(orm = nil)
114
- orm ||= set_orm
115
- puts "Copying sample #{orm} models into host application - resolves any collisions"
116
- seen, copied, duplicated = [], [], []
117
- Dir.glob(File.dirname(__FILE__) / ".." / ".." / "spec" / "models" / orm.to_s.downcase / MerbAdmin.glob_for(:model)).each do |source_filename|
118
- next if seen.include?(source_filename)
119
- destination_filename = Merb.dir_for(:model) / File.basename(source_filename)
120
- mirror_file(source_filename, destination_filename, copied, duplicated)
121
- seen << source_filename
122
- end
123
- copied.each { |f| puts "- copied #{f}" }
124
- duplicated.each { |f| puts "! duplicated override as #{f}" }
125
- end
126
-
127
- def copy_migrations(orm = nil)
128
- orm ||= set_orm
129
- puts "Copying sample #{orm} migrations into host application - resolves any collisions"
130
- seen, copied, duplicated = [], [], []
131
- Dir.glob(File.dirname(__FILE__) / ".." / ".." / "spec" / "migrations" / orm.to_s.downcase / "*.rb").each do |source_filename|
132
- next if seen.include?(source_filename)
133
- destination_filename = Merb.root / "schema" / "migrations" / File.basename(source_filename)
134
- mirror_file(source_filename, destination_filename, copied, duplicated)
135
- seen << source_filename
136
- end
137
- copied.each { |f| puts "- copied #{f}" }
138
- duplicated.each { |f| puts "! duplicated override as #{f}" }
139
- end
140
-
141
- def require_models
142
- Dir.glob(Merb.dir_for(:model) / Merb.glob_for(:model)).each do |model_filename|
143
- require model_filename
144
- end
145
- end
146
-
147
- def set_orm(orm = nil)
148
- orm || ENV['MERB_ORM'] || (Merb.orm != :none ? Merb.orm : nil) || :activerecord
149
- end
150
-
151
- def mirror_file(source, dest, copied = [], duplicated = [], postfix = '_override')
152
- base, rest = split_name(source)
153
- dst_dir = File.dirname(dest)
154
- dup_path = dst_dir / "#{base}#{postfix}.#{rest}"
155
- if File.file?(source)
156
- FileUtils.mkdir_p(dst_dir) unless File.directory?(dst_dir)
157
- if File.exists?(dest) && !File.exists?(dup_path) && !FileUtils.identical?(source, dest)
158
- # copy app-level override to *_override.ext
159
- FileUtils.copy_entry(dest, dup_path, false, false, true)
160
- duplicated << dup_path.relative_path_from(Merb.root)
161
- end
162
- # copy gem-level original to location
163
- if !File.exists?(dest) || (File.exists?(dest) && !FileUtils.identical?(source, dest))
164
- FileUtils.copy_entry(source, dest, false, false, true)
165
- copied << dest.relative_path_from(Merb.root)
166
- end
167
- end
168
- end
169
-
170
- def split_name(name)
171
- file_name = File.basename(name)
172
- mres = /^([^\/\.]+)\.(.+)$/i.match(file_name)
173
- mres.nil? ? [file_name, ''] : [mres[1], mres[2]]
174
- end
1
+ require 'abstract_model'
2
+
3
+ namespace :slices do
4
+ namespace :"merb-admin" do
5
+
6
+ # # Uncomment the following lines and edit the pre defined tasks
7
+ #
8
+ # # implement this to test for structural/code dependencies
9
+ # # like certain directories or availability of other files
10
+ # desc "Test for any dependencies"
11
+ # task :preflight do
12
+ # end
13
+ #
14
+ # # implement this to perform any database related setup steps
15
+ # desc "Migrate the database"
16
+ # task :migrate do
17
+ # end
18
+
19
+ desc "Copies sample models, copies and runs sample migrations, and loads sample data into your app"
20
+ task :activerecord => ["activerecord:copy_sample_models", "activerecord:copy_sample_migrations", "activerecord:migrate", "load_sample_data"]
21
+ namespace :activerecord do
22
+ desc "Copies sample models into your app"
23
+ task :copy_sample_models do
24
+ copy_models(:activerecord)
25
+ end
26
+
27
+ desc "Copies sample migrations into your app"
28
+ task :copy_sample_migrations do
29
+ copy_migrations(:activerecord)
30
+ end
31
+
32
+ desc "Migrate the database to the latest version"
33
+ task :migrate do
34
+ Rake::Task["db:migrate"].reenable
35
+ Rake::Task["db:migrate"].invoke
36
+ end
37
+ end
38
+
39
+ desc "Copies sample models, runs sample migrations, and loads sample data into your app"
40
+ task :datamapper => ["datamapper:copy_sample_models", "datamapper:migrate", "load_sample_data"]
41
+ namespace :datamapper do
42
+ desc "Copies sample models into your app"
43
+ task :copy_sample_models do
44
+ copy_models(:datamapper)
45
+ end
46
+
47
+ desc "Perform non destructive automigration"
48
+ task :migrate do
49
+ Rake::Task["db:automigrate"].reenable
50
+ Rake::Task["db:automigrate"].invoke
51
+ end
52
+ end
53
+
54
+ desc "Copies sample models, copies and runs sample migrations, and loads sample data"
55
+ task :sequel => ["sequel:copy_sample_models", "sequel:copy_sample_migrations", "sequel:migrate", "load_sample_data"]
56
+ namespace :sequel do
57
+ desc "Copies sample models into your app"
58
+ task :copy_sample_models do
59
+ copy_models(:sequel)
60
+ end
61
+
62
+ desc "Copies sample migrations into your app"
63
+ task :copy_sample_migrations do
64
+ copy_migrations(:sequel)
65
+ end
66
+
67
+ desc "Perform migration using migrations in schema/migrations"
68
+ task :migrate do
69
+ require 'sequel/extensions/migration'
70
+ Rake::Task["sequel:db:migrate"].reenable
71
+ Rake::Task["sequel:db:migrate"].invoke
72
+ end
73
+ end
74
+
75
+ desc "Loads sample data into your app"
76
+ task :load_sample_data do
77
+ load_data
78
+ end
79
+
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ def load_data
86
+ begin
87
+ require "mlb"
88
+ rescue LoadError => e
89
+ puts "LoadError: #{e}"
90
+ puts "gem install mlb -s http://gemcutter.org"
91
+ return
92
+ end
93
+
94
+ require_models
95
+
96
+ puts "Loading current MLB leagues, divisions, teams, and players"
97
+ MLB::Team.all.each do |mlb_team|
98
+ unless league = MerbAdmin::AbstractModel.new("League").first(:conditions => ["name = ?", mlb_team.league])
99
+ league = MerbAdmin::AbstractModel.new("League").create(:name => mlb_team.league)
100
+ end
101
+ unless division = MerbAdmin::AbstractModel.new("Division").first(:conditions => ["name = ?", mlb_team.division])
102
+ division = MerbAdmin::AbstractModel.new("Division").create(:name => mlb_team.division, :league => league)
103
+ end
104
+ unless team = MerbAdmin::AbstractModel.new("Team").first(:conditions => ["name = ?", mlb_team.name])
105
+ team = MerbAdmin::AbstractModel.new("Team").create(:name => mlb_team.name, :logo_url => mlb_team.logo_url, :manager => mlb_team.manager, :ballpark => mlb_team.ballpark, :mascot => mlb_team.mascot, :founded => mlb_team.founded, :wins => mlb_team.wins, :losses => mlb_team.losses, :win_percentage => ("%.3f" % (mlb_team.wins.to_f / (mlb_team.wins + mlb_team.losses))).to_f, :division => division, :league => league)
106
+ end
107
+ mlb_team.players.reject{|player| player.number.nil?}.each do |player|
108
+ MerbAdmin::AbstractModel.new("Player").create(:name => player.name, :number => player.number, :position => player.position, :team => team)
109
+ end
110
+ end
111
+ end
112
+
113
+ def copy_models(orm = nil)
114
+ orm ||= set_orm
115
+ puts "Copying sample #{orm} models into host application - resolves any collisions"
116
+ seen, copied, duplicated = [], [], []
117
+ Dir.glob(File.dirname(__FILE__) / ".." / ".." / "spec" / "models" / orm.to_s.downcase / MerbAdmin.glob_for(:model)).each do |source_filename|
118
+ next if seen.include?(source_filename)
119
+ destination_filename = Merb.dir_for(:model) / File.basename(source_filename)
120
+ mirror_file(source_filename, destination_filename, copied, duplicated)
121
+ seen << source_filename
122
+ end
123
+ copied.each { |f| puts "- copied #{f}" }
124
+ duplicated.each { |f| puts "! duplicated override as #{f}" }
125
+ end
126
+
127
+ def copy_migrations(orm = nil)
128
+ orm ||= set_orm
129
+ puts "Copying sample #{orm} migrations into host application - resolves any collisions"
130
+ seen, copied, duplicated = [], [], []
131
+ Dir.glob(File.dirname(__FILE__) / ".." / ".." / "spec" / "migrations" / orm.to_s.downcase / "*.rb").each do |source_filename|
132
+ next if seen.include?(source_filename)
133
+ destination_filename = Merb.root / "schema" / "migrations" / File.basename(source_filename)
134
+ mirror_file(source_filename, destination_filename, copied, duplicated)
135
+ seen << source_filename
136
+ end
137
+ copied.each { |f| puts "- copied #{f}" }
138
+ duplicated.each { |f| puts "! duplicated override as #{f}" }
139
+ end
140
+
141
+ def require_models
142
+ Dir.glob(Merb.dir_for(:model) / Merb.glob_for(:model)).each do |model_filename|
143
+ require model_filename
144
+ end
145
+ end
146
+
147
+ def set_orm(orm = nil)
148
+ orm || ENV['MERB_ORM'] || (Merb.orm != :none ? Merb.orm : nil) || :activerecord
149
+ end
150
+
151
+ def mirror_file(source, dest, copied = [], duplicated = [], postfix = '_override')
152
+ base, rest = split_name(source)
153
+ dst_dir = File.dirname(dest)
154
+ dup_path = dst_dir / "#{base}#{postfix}.#{rest}"
155
+ if File.file?(source)
156
+ FileUtils.mkdir_p(dst_dir) unless File.directory?(dst_dir)
157
+ if File.exists?(dest) && !File.exists?(dup_path) && !FileUtils.identical?(source, dest)
158
+ # copy app-level override to *_override.ext
159
+ FileUtils.copy_entry(dest, dup_path, false, false, true)
160
+ duplicated << dup_path.relative_path_from(Merb.root)
161
+ end
162
+ # copy gem-level original to location
163
+ if !File.exists?(dest) || (File.exists?(dest) && !FileUtils.identical?(source, dest))
164
+ FileUtils.copy_entry(source, dest, false, false, true)
165
+ copied << dest.relative_path_from(Merb.root)
166
+ end
167
+ end
168
+ end
169
+
170
+ def split_name(name)
171
+ file_name = File.basename(name)
172
+ mres = /^([^\/\.]+)\.(.+)$/i.match(file_name)
173
+ mres.nil? ? [file_name, ''] : [mres[1], mres[2]]
174
+ end