engine_room 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. data/.gitignore +10 -0
  2. data/Gemfile +4 -0
  3. data/Gemfile.lock +159 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.rdoc +44 -0
  6. data/Rakefile +30 -0
  7. data/TODO +3 -0
  8. data/app/controllers/engine_room/content_controller.rb +117 -0
  9. data/app/controllers/engine_room/fields_controller.rb +119 -0
  10. data/app/controllers/engine_room/models_controller.rb +89 -0
  11. data/app/controllers/engine_room/pages_controller.rb +17 -0
  12. data/app/controllers/engine_room/sections_controller.rb +74 -0
  13. data/app/controllers/engine_room/users_controller.rb +84 -0
  14. data/app/controllers/er_devise/passwords_controller.rb +6 -0
  15. data/app/controllers/er_devise/registrations_controller.rb +8 -0
  16. data/app/controllers/er_devise/sessions_controller.rb +6 -0
  17. data/app/helpers/engine_room/content_helper.rb +103 -0
  18. data/app/helpers/engine_room/models_helper.rb +21 -0
  19. data/app/models/field.rb +89 -0
  20. data/app/models/section.rb +68 -0
  21. data/app/models/user.rb +31 -0
  22. data/app/views/engine_room/content/_form.html.erb +13 -0
  23. data/app/views/engine_room/content/_overview.html.erb +28 -0
  24. data/app/views/engine_room/content/_sidebar.html.erb +14 -0
  25. data/app/views/engine_room/content/edit.html.erb +38 -0
  26. data/app/views/engine_room/content/index.html.erb +1 -0
  27. data/app/views/engine_room/content/new.html.erb +1 -0
  28. data/app/views/engine_room/content/overview.html.erb +3 -0
  29. data/app/views/engine_room/fields/_form.html.erb +57 -0
  30. data/app/views/engine_room/fields/edit.html.erb +1 -0
  31. data/app/views/engine_room/fields/new.html.erb +1 -0
  32. data/app/views/engine_room/models/_form.html.erb +0 -0
  33. data/app/views/engine_room/models/edit.html.erb +14 -0
  34. data/app/views/engine_room/models/index.html.erb +16 -0
  35. data/app/views/engine_room/models/new.html.erb +14 -0
  36. data/app/views/engine_room/models/overview.html.erb +14 -0
  37. data/app/views/engine_room/pages/dashboard.html.erb +1 -0
  38. data/app/views/engine_room/pages/settings.html.erb +1 -0
  39. data/app/views/engine_room/sections/_form.html.erb +34 -0
  40. data/app/views/engine_room/sections/_sidebar.html.erb +11 -0
  41. data/app/views/engine_room/sections/edit.html.erb +81 -0
  42. data/app/views/engine_room/sections/index.html.erb +3 -0
  43. data/app/views/engine_room/sections/new.html.erb +1 -0
  44. data/app/views/engine_room/users/_form.html.erb +47 -0
  45. data/app/views/engine_room/users/edit.html.erb +11 -0
  46. data/app/views/engine_room/users/index.html.erb +24 -0
  47. data/app/views/engine_room/users/new.html.erb +1 -0
  48. data/app/views/er_devise/mailer/confirmation_instructions.html.erb +5 -0
  49. data/app/views/er_devise/mailer/reset_password_instructions.html.erb +8 -0
  50. data/app/views/er_devise/mailer/unlock_instructions.html.erb +7 -0
  51. data/app/views/er_devise/passwords/edit.html.erb +16 -0
  52. data/app/views/er_devise/passwords/new.html.erb +13 -0
  53. data/app/views/er_devise/registrations/edit.html.erb +42 -0
  54. data/app/views/er_devise/registrations/new.html.erb +21 -0
  55. data/app/views/er_devise/sessions/new.html.erb +23 -0
  56. data/app/views/er_devise/shared/_links.erb +19 -0
  57. data/app/views/layouts/engine_room.html.erb +103 -0
  58. data/app/views/layouts/login.html.erb +71 -0
  59. data/app/views/shared/_content_layout_selector.html.erb +45 -0
  60. data/app/views/shared/_error_messages.html.erb +17 -0
  61. data/config/initializers/devise.rb +146 -0
  62. data/config/initializers/locale.rb +5 -0
  63. data/config/locales/devise.de.yml +42 -0
  64. data/config/locales/devise.en.yml +39 -0
  65. data/config/locales/engine_room.de.yml +33 -0
  66. data/config/locales/engine_room.en.yml +42 -0
  67. data/config/routes.rb +62 -0
  68. data/engine_room.gemspec +35 -0
  69. data/lib/engine_room.rb +17 -0
  70. data/lib/engine_room/action_controller/controller_methods.rb +21 -0
  71. data/lib/engine_room/action_view/view_methods.rb +8 -0
  72. data/lib/engine_room/active_record/record_methods.rb +18 -0
  73. data/lib/engine_room/crummy/custom_renderer.rb +63 -0
  74. data/lib/engine_room/engine.rb +40 -0
  75. data/lib/engine_room/railties/tasks.rake +6 -0
  76. data/lib/engine_room/version.rb +3 -0
  77. data/lib/generators/engine_room/create_image_generator.rb +26 -0
  78. data/lib/generators/engine_room/setup_generator.rb +30 -0
  79. data/lib/generators/engine_room/templates/001_devise_create_users.rb +28 -0
  80. data/lib/generators/engine_room/templates/002_create_sections.rb +16 -0
  81. data/lib/generators/engine_room/templates/003_create_fields.rb +18 -0
  82. data/lib/generators/engine_room/templates/004_create_images.rb +19 -0
  83. data/lib/generators/engine_room/templates/image.rb +4 -0
  84. data/lib/tasks/dummy.rake +45 -0
  85. data/lib/tasks/templates/migrate/050_create_dogs.rb +18 -0
  86. data/lib/tasks/templates/models/dog.rb +3 -0
  87. data/public/images/er/accept.png +0 -0
  88. data/public/images/er/add.png +0 -0
  89. data/public/images/er/add_small.png +0 -0
  90. data/public/images/er/ajax-loader.gif +0 -0
  91. data/public/images/er/attach.png +0 -0
  92. data/public/images/er/back_disabled.jpg +0 -0
  93. data/public/images/er/back_enabled.jpg +0 -0
  94. data/public/images/er/bg/header_gradient_center.jpg +0 -0
  95. data/public/images/er/bg/header_gradient_left.jpg +0 -0
  96. data/public/images/er/bg/header_gradient_right.jpg +0 -0
  97. data/public/images/er/cancel.png +0 -0
  98. data/public/images/er/cog.png +0 -0
  99. data/public/images/er/database_add.png +0 -0
  100. data/public/images/er/delete.png +0 -0
  101. data/public/images/er/delete_small.png +0 -0
  102. data/public/images/er/down_arrow.gif +0 -0
  103. data/public/images/er/down_arrow.png +0 -0
  104. data/public/images/er/email.png +0 -0
  105. data/public/images/er/exclamation.png +0 -0
  106. data/public/images/er/forward_disabled.jpg +0 -0
  107. data/public/images/er/forward_enabled.jpg +0 -0
  108. data/public/images/er/help.png +0 -0
  109. data/public/images/er/image_add.png +0 -0
  110. data/public/images/er/information.png +0 -0
  111. data/public/images/er/link.png +0 -0
  112. data/public/images/er/loader.gif +0 -0
  113. data/public/images/er/telephone.png +0 -0
  114. data/public/images/er/up_arrow.gif +0 -0
  115. data/public/images/er/up_arrow.png +0 -0
  116. data/public/images/er/user.png +0 -0
  117. data/public/images/er/wrench.png +0 -0
  118. data/public/images/er/zoom.png +0 -0
  119. data/public/javascripts/er/dd_belatedpng.js +328 -0
  120. data/public/javascripts/er/fancybox/blank.gif +0 -0
  121. data/public/javascripts/er/fancybox/fancy_close.png +0 -0
  122. data/public/javascripts/er/fancybox/fancy_loading.png +0 -0
  123. data/public/javascripts/er/fancybox/fancy_nav_left.png +0 -0
  124. data/public/javascripts/er/fancybox/fancy_nav_right.png +0 -0
  125. data/public/javascripts/er/fancybox/fancy_shadow_e.png +0 -0
  126. data/public/javascripts/er/fancybox/fancy_shadow_n.png +0 -0
  127. data/public/javascripts/er/fancybox/fancy_shadow_ne.png +0 -0
  128. data/public/javascripts/er/fancybox/fancy_shadow_nw.png +0 -0
  129. data/public/javascripts/er/fancybox/fancy_shadow_s.png +0 -0
  130. data/public/javascripts/er/fancybox/fancy_shadow_se.png +0 -0
  131. data/public/javascripts/er/fancybox/fancy_shadow_sw.png +0 -0
  132. data/public/javascripts/er/fancybox/fancy_shadow_w.png +0 -0
  133. data/public/javascripts/er/fancybox/fancy_title_left.png +0 -0
  134. data/public/javascripts/er/fancybox/fancy_title_main.png +0 -0
  135. data/public/javascripts/er/fancybox/fancy_title_over.png +0 -0
  136. data/public/javascripts/er/fancybox/fancy_title_right.png +0 -0
  137. data/public/javascripts/er/fancybox/fancybox-x.png +0 -0
  138. data/public/javascripts/er/fancybox/fancybox-y.png +0 -0
  139. data/public/javascripts/er/fancybox/fancybox.png +0 -0
  140. data/public/javascripts/er/fancybox/jquery.fancybox-1.3.4.css +359 -0
  141. data/public/javascripts/er/fancybox/jquery.fancybox-1.3.4.pack.js +46 -0
  142. data/public/javascripts/er/jquery-1.4.4.min.js +167 -0
  143. data/public/javascripts/er/jquery-ui-1.8.6.custom.min.js +477 -0
  144. data/public/javascripts/er/modernizr-1.5.min.js +28 -0
  145. data/public/javascripts/er/plugins.js +48 -0
  146. data/public/javascripts/er/rails.js +156 -0
  147. data/public/javascripts/er/script.js +50 -0
  148. data/public/stylesheets/.gitkeep +0 -0
  149. data/public/stylesheets/admin.css +417 -0
  150. data/public/stylesheets/boilerplate.css +264 -0
  151. data/public/stylesheets/handheld.css +7 -0
  152. data/public/stylesheets/login.css +125 -0
  153. data/public/stylesheets/skin.css +2 -0
  154. data/public/stylesheets/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  155. data/public/stylesheets/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
  156. data/public/stylesheets/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  157. data/public/stylesheets/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  158. data/public/stylesheets/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  159. data/public/stylesheets/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  160. data/public/stylesheets/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  161. data/public/stylesheets/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  162. data/public/stylesheets/smoothness/images/ui-icons_222222_256x240.png +0 -0
  163. data/public/stylesheets/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
  164. data/public/stylesheets/smoothness/images/ui-icons_454545_256x240.png +0 -0
  165. data/public/stylesheets/smoothness/images/ui-icons_888888_256x240.png +0 -0
  166. data/public/stylesheets/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
  167. data/public/stylesheets/smoothness/jquery-ui-1.8.6.custom.css +479 -0
  168. data/spec/controllers/content_controller_spec.rb +132 -0
  169. data/spec/controllers/fields_controller_spec.rb +103 -0
  170. data/spec/controllers/models_controller_spec.rb +65 -0
  171. data/spec/controllers/pages_controller_spec.rb +40 -0
  172. data/spec/controllers/sections_controller_spec.rb +124 -0
  173. data/spec/dummy/Rakefile +7 -0
  174. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  175. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  176. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  177. data/spec/dummy/config.ru +4 -0
  178. data/spec/dummy/config/application.rb +45 -0
  179. data/spec/dummy/config/boot.rb +10 -0
  180. data/spec/dummy/config/database.yml +22 -0
  181. data/spec/dummy/config/environment.rb +5 -0
  182. data/spec/dummy/config/environments/development.rb +26 -0
  183. data/spec/dummy/config/environments/production.rb +49 -0
  184. data/spec/dummy/config/environments/test.rb +35 -0
  185. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  186. data/spec/dummy/config/initializers/inflections.rb +10 -0
  187. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  188. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  189. data/spec/dummy/config/initializers/session_store.rb +8 -0
  190. data/spec/dummy/config/locales/en.yml +5 -0
  191. data/spec/dummy/config/routes.rb +58 -0
  192. data/spec/dummy/public/404.html +26 -0
  193. data/spec/dummy/public/422.html +26 -0
  194. data/spec/dummy/public/500.html +26 -0
  195. data/spec/dummy/public/favicon.ico +0 -0
  196. data/spec/dummy/public/javascripts/application.js +2 -0
  197. data/spec/dummy/public/javascripts/controls.js +965 -0
  198. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  199. data/spec/dummy/public/javascripts/effects.js +1123 -0
  200. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  201. data/spec/dummy/public/javascripts/rails.js +175 -0
  202. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  203. data/spec/dummy/script/rails +6 -0
  204. data/spec/engine_room_spec.rb +7 -0
  205. data/spec/factories.rb +32 -0
  206. data/spec/integration/navigation_spec.rb +9 -0
  207. data/spec/models/dog_spec.rb +14 -0
  208. data/spec/models/field_spec.rb +74 -0
  209. data/spec/models/section_spec.rb +70 -0
  210. data/spec/models/user_spec.rb +77 -0
  211. data/spec/routing/models_routing_spec.rb +66 -0
  212. data/spec/spec_helper.rb +39 -0
  213. data/spec/support/devise.rb +3 -0
  214. metadata +492 -0
@@ -0,0 +1,89 @@
1
+ module EngineRoom
2
+ class ModelsController < ApplicationController
3
+ before_filter :authenticate_er_devise_user!
4
+ before_filter :authorize
5
+ before_filter :check_model_name, :except => [:overview]
6
+
7
+ layout 'engine_room'
8
+
9
+ add_crumb "Models", '/admin/models'
10
+
11
+ unloadable
12
+
13
+ def check_model_name
14
+ @modelname = params[:modelname]
15
+ begin
16
+ @model = get_model(@modelname)
17
+ rescue NameError
18
+ flash[:error] = "No model with given name was found!"
19
+ redirect_to :action => :overview
20
+ end
21
+ end
22
+
23
+
24
+ def overview
25
+ @sections = Section.all
26
+ end
27
+
28
+
29
+ def overview2
30
+ @models = []
31
+ missing_models = []
32
+ missing_tables = []
33
+ Section.all.each do |section|
34
+ begin
35
+ model = get_model(section.model_name)
36
+ if model.table_exists?
37
+ @models << model
38
+ else
39
+ missing_tables << section.model_name
40
+ end
41
+ rescue NameError
42
+ missing_models << section.model_name
43
+ end
44
+ end
45
+
46
+ flash_alert = ""
47
+ flash_alert += "Table(s) not found: '#{missing_tables.uniq.join("', '")}'. " unless missing_tables.empty?
48
+ flash_alert += "Model(s) not found: '#{missing_models.uniq.join("', '")}'." unless missing_models.empty?
49
+ flash[:alert] = flash_alert unless flash_alert.empty?
50
+ end
51
+
52
+
53
+ def index
54
+ @elements = @model.find(:all)
55
+ add_crumb @modelname, {:controller => 'engine_room/models', :action => :index, :modelname => @modelname}
56
+ end
57
+
58
+ def show
59
+ @element = @model.find(params[:id])
60
+ end
61
+
62
+ def new
63
+ @element = @model.new
64
+ add_crumb @modelname, {:controller => 'engine_room/models', :action => :index, :modelname => @modelname}
65
+ add_crumb "Create New #{@modelname}"
66
+ end
67
+
68
+ def edit
69
+ @element = @model.find(params[:id])
70
+ end
71
+
72
+ def update
73
+ @element = @model.find(params[:id])
74
+ if @element.update_attributes(params[@modelname.downcase])
75
+ flash[:notice] = "#{@modelname} successfully updated."
76
+ redirect_to :action => :edit
77
+ else
78
+ render 'edit'
79
+ end
80
+ end
81
+
82
+ private
83
+
84
+ def get_model model_name
85
+ model = Object.const_get(model_name.classify)
86
+ end
87
+
88
+ end
89
+ end
@@ -0,0 +1,17 @@
1
+ module EngineRoom
2
+ class PagesController < ApplicationController
3
+ before_filter :authenticate_er_devise_user!
4
+
5
+ layout 'engine_room'
6
+
7
+ unloadable
8
+
9
+ def dashboard
10
+ end
11
+
12
+ def sections
13
+ @sections = Section.all
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,74 @@
1
+ module EngineRoom
2
+ class SectionsController < ApplicationController
3
+ before_filter :authenticate_er_devise_user!
4
+ before_filter :authorize
5
+ before_filter :init_sections
6
+
7
+ layout 'engine_room'
8
+
9
+ unloadable
10
+
11
+ def init_sections
12
+ @sections = Section.order('sort_order ASC')
13
+ @layout = 'columns'
14
+ end
15
+
16
+ # GET /section_configs
17
+ def index
18
+ @sections = Section.order('sort_order ASC')
19
+ # index.html.erb
20
+ end
21
+
22
+ # GET /section_config/1
23
+ def show
24
+ @section = Section.find(params[:id])
25
+ # show.html.erb
26
+ end
27
+
28
+ # GET /section_config/new
29
+ def new
30
+ @section = Section.new
31
+ add_crumb 'Create New Section'
32
+ end
33
+
34
+ # GET /section/1/edit
35
+ def edit
36
+ @section = Section.find(params[:id])
37
+ add_crumb(@section.name.titleize, edit_engine_room_section_path(params[:id]))
38
+ end
39
+
40
+ # POST /section
41
+ def create
42
+ @section = Section.new(params[:section])
43
+ if @section.save
44
+ flash[:notice] = 'Section was successfully created.'
45
+ redirect_to :action => :index
46
+ else
47
+ add_crumb('Create New Section', new_engine_room_section_path)
48
+ render :action => :new
49
+ end
50
+ end
51
+
52
+ # PUT /section/1
53
+ def update
54
+ @section = Section.find(params[:id])
55
+ if @section.update_attributes(params[:section])
56
+ flash[:notice] = 'Section was successfully updated.'
57
+ redirect_to :action => :index
58
+ else
59
+ add_crumb(@section.name.titleize, edit_engine_room_section_path(params[:id]))
60
+ render :action => :edit
61
+ end
62
+ end
63
+
64
+ # DELETE /section/1
65
+ def destroy
66
+ @section = Section.find(params[:id])
67
+ @section.destroy
68
+
69
+ flash[:notice] = 'Section was successfully deleted.'
70
+ redirect_to :action => :index
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,84 @@
1
+ module EngineRoom
2
+ class UsersController < ApplicationController
3
+ before_filter :authenticate_er_devise_user!
4
+ before_filter :authorize, :except => [:edit_current_user, :update_current_user, :destroy_current_user]
5
+
6
+ layout 'engine_room'
7
+
8
+ add_crumb "Users", '/admin/users'
9
+
10
+ unloadable
11
+
12
+ # GET /users
13
+ def index
14
+ @users = User.order('username ASC')
15
+ # index.html.erb
16
+ end
17
+
18
+ def new
19
+ @user = User.new
20
+ add_crumb("Create User")
21
+ end
22
+
23
+ def create
24
+ @user = User.new(params[:user])
25
+ if @user.save
26
+ flash[:notice] = 'User was successfully created.'
27
+ redirect_to :action => :index
28
+ else
29
+ @user.clean_up_passwords
30
+ add_crumb("Create User")
31
+ render :action => :new
32
+ end
33
+ end
34
+
35
+ def edit
36
+ @user = User.find(params[:id])
37
+ add_crumb("Edit User")
38
+ end
39
+
40
+ def edit_current_user
41
+ @user = current_user
42
+ render :edit
43
+ end
44
+
45
+ def update
46
+ @user = User.find(params[:id])
47
+ update_user
48
+ end
49
+
50
+ def update_current_user
51
+ @user = current_user
52
+ update_user
53
+ end
54
+
55
+ def destroy
56
+ @user = User.find(params[:id])
57
+ @user.destroy
58
+
59
+ flash[:notice] = 'User was successfully deleted.'
60
+ redirect_to :action => :index
61
+ end
62
+
63
+ def destroy_current_user
64
+ @user = current_user
65
+ @user.destroy
66
+
67
+ redirect_to destroy_er_devise_user_session_url
68
+ end
69
+
70
+ private
71
+
72
+ def update_user
73
+ if @user.update_with_admin_check(params[:user], current_user.is_admin?)
74
+ flash[:notice] = 'User was successfully updated.'
75
+ redirect_to :action => params[:action]=="update" ? :index : :edit_current_user
76
+ else
77
+ @user.clean_up_passwords
78
+ add_crumb("Edit User")
79
+ render :action => :edit
80
+ end
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,6 @@
1
+ module ErDevise
2
+ class PasswordsController < Devise::PasswordsController
3
+ layout 'login'
4
+ prepend_view_path(__FILE__ + "../../views")
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ module ErDevise
2
+ class RegistrationsController < Devise::RegistrationsController
3
+ before_filter :authenticate_er_devise_user!
4
+ layout 'engine_room'
5
+ prepend_view_path(__FILE__ + "../../views")
6
+
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ module ErDevise
2
+ class SessionsController < Devise::SessionsController
3
+ layout 'login'
4
+ prepend_view_path(__FILE__ + "../../views")
5
+ end
6
+ end
@@ -0,0 +1,103 @@
1
+ module EngineRoom
2
+ module ContentHelper
3
+
4
+ def sortable(column)
5
+ css_class = column == sort_column ? "current #{sort_direction}" : nil
6
+ direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
7
+ link_to column.titleize, {:sort => column, :direction => direction}, {:class => css_class}
8
+ end
9
+
10
+
11
+ def field_to_overview(field, element, section, bt_section_name=nil, bt_id=nil)
12
+ html = element[field.column]
13
+ if html.class.name == "Time"
14
+ html = l( html, :format => :short )
15
+ end
16
+
17
+ case field.field_type
18
+ when "paperclip_field"
19
+ img_name = element["#{field.column}_file_name"]
20
+ html = image_tag( element.send( field.column ).url, :title => img_name, :height => "50px" )
21
+ end
22
+
23
+ if field.overview_link
24
+ link_options = {:controller => 'engine_room/content', :section_name => section.name, :id => element.id, :action => :edit}
25
+ if bt_section_name && bt_id
26
+ link_options[:bt_section] = bt_section_name
27
+ link_options[:bt_id] = bt_id
28
+ end
29
+ html = link_to html, link_options
30
+ end
31
+ return content_tag(:td, html, nil, false)
32
+ end
33
+
34
+
35
+ def field_to_form(field, element, form)
36
+ html = form.label field.column, field.column.humanize
37
+
38
+ case field.field_type
39
+ when "hidden"
40
+ html += form.hidden_field field.column
41
+ when "read_only"
42
+ html += element[field.column]
43
+ when "text_field"
44
+ html += form.text_field field.column
45
+ when "email_field"
46
+ html += form.text_field field.column
47
+ when "number_field"
48
+ html += form.number_field field.column
49
+ when "date_field"
50
+ html += form.date_select field.column
51
+ when "text_area"
52
+ html += form.text_area field.column, :rows => 4
53
+ when "boolean"
54
+ html += form.check_box field.column
55
+ when "enum"
56
+ html += form.select field.column
57
+ when "belongs_to"
58
+ html += form.select field.column, options_for_belongs_to(field, element)
59
+ when "paperclip_field"
60
+ if element.respond_to?("#{field.column}_file_name")
61
+ img_name = element["#{field.column}_file_name"]
62
+ html += image_tag( element.send( field.column ).url, :title => img_name, :height => "50px" )
63
+ html += content_tag(:p, "file name: #{img_name}")
64
+ end
65
+ html += form.file_field field.column
66
+ end
67
+
68
+ unless field.help.blank?
69
+ html += content_tag(:i, field.help)
70
+ end
71
+
72
+ return content_tag(:div, html, :class => "field")
73
+ end
74
+
75
+ # get all elements that element has_many of (paginated)
76
+ def has_many_elements(element, target_model)
77
+ element.class.reflect_on_all_associations(:has_many).each do |assoc|
78
+ if assoc.name.to_s == target_model.downcase.pluralize
79
+ model = Object.const_get(target_model.singularize.camelize)
80
+ return element.send(target_model.downcase.pluralize).paginate(:per_page => 10, :page => 1)
81
+ end
82
+ end
83
+ return []
84
+ end
85
+
86
+ private
87
+
88
+ def options_for_belongs_to(field, element)
89
+
90
+ target_class_name = field.column.match(/(.*)_id$/) ? $1 : ""
91
+
92
+ element.class.reflect_on_all_associations(:belongs_to).each do |assoc|
93
+ if assoc.name.to_s == target_class_name
94
+ model = Object.const_get(target_class_name.singularize.camelize)
95
+ return options_for_select(model.order("#{field.target_display_column}").collect {|r| [r[field.target_display_column], r.id] }, element[field.column])
96
+ end
97
+ end
98
+
99
+ return options_for_select([])
100
+ end
101
+
102
+ end
103
+ end
@@ -0,0 +1,21 @@
1
+ module EngineRoom
2
+ module ModelsHelper
3
+
4
+ def get_form_field_for_column(form, element, column)
5
+
6
+ if column.primary || column.name == "created_at" || column.name == "updated_at"
7
+ element[column.name]
8
+ else
9
+
10
+ if [:string, :integer, :datetime].include?(column.type)
11
+ form.text_field column.name
12
+ elsif [:text].include?(column.type)
13
+ form.text_area column.name, :class => "bespind", :rows => 5
14
+ else
15
+ column.type
16
+ end
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,89 @@
1
+ class ValidFieldTypeValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ record.errors.add(attribute, "is not a valid field type.") unless
4
+ Field.field_types.include?(record[attribute])
5
+ end
6
+ end
7
+
8
+ class Field < ActiveRecord::Base
9
+ belongs_to :section
10
+ attr_accessible :column, :field_type, :display_type, :help, :sort_order, :section_id, :overview_link #, :display_target_column
11
+ serialize :options
12
+
13
+ validates :column, :presence => true
14
+ validates :field_type, :presence => true, :valid_field_type => true
15
+ validates :display_type, :presence => true
16
+ validates :section_id, :presence => true
17
+
18
+ default_scope :order => 'fields.sort_order ASC'
19
+
20
+ @@field_config = {
21
+ 'hidden' => [],
22
+ 'read_only' => [],
23
+ 'text_field' => [],
24
+ 'email_field' => [],
25
+ 'number_field' => [],
26
+ 'date_field' => ['format'],
27
+ 'text_area' => [],
28
+ 'boolean' => [],
29
+ 'enum' => ['enum_values'],
30
+ 'paperclip_field' => [],
31
+ 'belongs_to' => ['target_display_column'],
32
+ 'has_many' => ['target_model', 'target_section_id']
33
+ }
34
+
35
+ # returns array of possible field types
36
+ def self.field_types
37
+ @@field_config.keys
38
+ end
39
+
40
+ def option_fields
41
+ self.field_type.blank? || !@@field_config.has_key?(self.field_type) ? [] : @@field_config[self.field_type]
42
+ end
43
+
44
+ def self.valid_options
45
+ options = []
46
+ @@field_config.each_value do |val|
47
+ options.concat(val)
48
+ end
49
+ return options
50
+ end
51
+
52
+
53
+ def overview_link
54
+ return false if self.options.blank?
55
+ is_link = self.options[:overview_link]
56
+ object_to_boolean(is_link)
57
+ end
58
+
59
+ def overview_link=(is_link)
60
+ self.options = Hash.new if self.options.blank?
61
+ self.options[:overview_link] = object_to_boolean(is_link)
62
+ end
63
+
64
+
65
+ private
66
+
67
+ # overriding for dynamic attr_accessible access to options
68
+ def mass_assignment_authorizer
69
+ super + Field.valid_options
70
+ end
71
+
72
+ def self.define_all_options
73
+ valid_options.each { |opt| define_option(opt) }
74
+ end
75
+
76
+ def self.define_option(name)
77
+ define_method(name) do
78
+ return false if self.options.blank?
79
+ self.options[name]
80
+ end
81
+ define_method("#{name}=") do |val|
82
+ self.options = Hash.new if self.options.blank?
83
+ self.options[name] = val
84
+ end
85
+ end
86
+
87
+ define_all_options
88
+
89
+ end