alberich 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (242) hide show
  1. data/Gemfile +18 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +37 -0
  4. data/Rakefile +34 -0
  5. data/alberich.gemspec +34 -0
  6. data/app/assets/javascripts/alberich/application.js +15 -0
  7. data/app/assets/javascripts/alberich/permissions.js +2 -0
  8. data/app/assets/javascripts/alberich/privileges.js +2 -0
  9. data/app/assets/javascripts/alberich/roles.js +2 -0
  10. data/app/assets/stylesheets/alberich/application.css +13 -0
  11. data/app/assets/stylesheets/alberich/permissions.css +4 -0
  12. data/app/assets/stylesheets/alberich/privileges.css +4 -0
  13. data/app/assets/stylesheets/alberich/roles.css +4 -0
  14. data/app/assets/stylesheets/scaffold.css +56 -0
  15. data/app/controllers/alberich/application_controller.rb +4 -0
  16. data/app/controllers/alberich/application_controller_helper.rb +118 -0
  17. data/app/controllers/alberich/permissions_controller.rb +211 -0
  18. data/app/controllers/alberich/privileges_controller.rb +105 -0
  19. data/app/controllers/alberich/roles_controller.rb +97 -0
  20. data/app/helpers/alberich/application_helper.rb +4 -0
  21. data/app/helpers/alberich/permissions_helper.rb +4 -0
  22. data/app/helpers/alberich/privileges_helper.rb +4 -0
  23. data/app/helpers/alberich/roles_helper.rb +4 -0
  24. data/app/models/alberich/base_permission_object.rb +42 -0
  25. data/app/models/alberich/derived_permission.rb +25 -0
  26. data/app/models/alberich/entity.rb +27 -0
  27. data/app/models/alberich/entity_target_observer.rb +16 -0
  28. data/app/models/alberich/permission.rb +59 -0
  29. data/app/models/alberich/permission_session.rb +33 -0
  30. data/app/models/alberich/permissioned_object.rb +139 -0
  31. data/app/models/alberich/privilege.rb +29 -0
  32. data/app/models/alberich/role.rb +37 -0
  33. data/app/models/alberich/session_entity.rb +15 -0
  34. data/app/views/alberich/permissions/_form.html.haml +27 -0
  35. data/app/views/alberich/permissions/_list.html.haml +1 -0
  36. data/app/views/alberich/permissions/_objects.html.haml +38 -0
  37. data/app/views/alberich/permissions/_permissions.html.haml +45 -0
  38. data/app/views/alberich/permissions/index.html.haml +2 -0
  39. data/app/views/alberich/permissions/new.html.haml +5 -0
  40. data/app/views/alberich/permissions/show.html.haml +12 -0
  41. data/app/views/alberich/privileges/_form.html.haml +19 -0
  42. data/app/views/alberich/privileges/_list.html.haml +17 -0
  43. data/app/views/alberich/privileges/create.html.haml +2 -0
  44. data/app/views/alberich/privileges/destroy.html.haml +2 -0
  45. data/app/views/alberich/privileges/edit.html.haml +5 -0
  46. data/app/views/alberich/privileges/index.html.haml +5 -0
  47. data/app/views/alberich/privileges/new.html.haml +5 -0
  48. data/app/views/alberich/privileges/show.html.haml +12 -0
  49. data/app/views/alberich/privileges/update.html.haml +2 -0
  50. data/app/views/alberich/roles/_form.html.haml +24 -0
  51. data/app/views/alberich/roles/edit.html.haml +7 -0
  52. data/app/views/alberich/roles/index.html.haml +23 -0
  53. data/app/views/alberich/roles/new.html.haml +5 -0
  54. data/app/views/alberich/roles/show.html.haml +16 -0
  55. data/app/views/layouts/alberich/application.html.erb +14 -0
  56. data/config/initializers/haml.rb +1 -0
  57. data/config/routes.rb +17 -0
  58. data/db/migrate/20120925162242_create_alberich_roles.rb +12 -0
  59. data/db/migrate/20121022223626_create_alberich_privileges.rb +12 -0
  60. data/db/migrate/20121023051301_create_alberich_base_permission_objects.rb +9 -0
  61. data/db/migrate/20121023233648_create_alberich_permission_sessions.rb +11 -0
  62. data/db/migrate/20121027023136_create_alberich_entities.rb +34 -0
  63. data/db/migrate/20121204205213_create_alberich_session_entities.rb +12 -0
  64. data/db/migrate/20121205180518_create_alberich_permissions.rb +13 -0
  65. data/db/migrate/20130107043252_create_alberich_derived_permissions.rb +18 -0
  66. data/lib/alberich.rb +10 -0
  67. data/lib/alberich/#version.rb# +3 -0
  68. data/lib/alberich/engine.rb +10 -0
  69. data/lib/alberich/version.rb +3 -0
  70. data/lib/generators/alberich/install_generator.rb +15 -0
  71. data/lib/generators/alberich/templates/README +6 -0
  72. data/lib/generators/alberich/templates/alberich.rb +11 -0
  73. data/lib/tasks/alberich_tasks.rake +4 -0
  74. data/spec/controllers/alberich/permissions_controller_spec.rb +112 -0
  75. data/spec/controllers/alberich/privileges_controller_spec.rb +131 -0
  76. data/spec/controllers/alberich/roles_controller_spec.rb +130 -0
  77. data/spec/factories/alberich/permission.rb +51 -0
  78. data/spec/factories/alberich/permission_session.rb +7 -0
  79. data/spec/factories/alberich/privilege.rb +6 -0
  80. data/spec/factories/alberich/role.rb +103 -0
  81. data/spec/factories/child_resource.rb +14 -0
  82. data/spec/factories/child_resource.rb~ +7 -0
  83. data/spec/factories/global_resource.rb +11 -0
  84. data/spec/factories/global_resource.rb~ +25 -0
  85. data/spec/factories/parent_resource.rb +12 -0
  86. data/spec/factories/parent_resource.rb~ +7 -0
  87. data/spec/factories/standalone_resource.rb +7 -0
  88. data/spec/factories/standalone_resource.rb~ +11 -0
  89. data/spec/factories/user.rb +30 -0
  90. data/spec/factories/user_group.rb +8 -0
  91. data/spec/models/alberich/derived_permission_spec.rb +34 -0
  92. data/spec/models/alberich/entity_spec.rb +15 -0
  93. data/spec/models/alberich/permission_spec.rb +133 -0
  94. data/spec/models/alberich/privilege_spec.rb +39 -0
  95. data/spec/models/alberich/role_spec.rb +33 -0
  96. data/spec/models/alberich/session_entity_spec.rb +24 -0
  97. data/spec/spec_helper.rb +81 -0
  98. data/spec/support/routes.rb +41 -0
  99. data/test/dummy/README.rdoc +261 -0
  100. data/test/dummy/Rakefile +7 -0
  101. data/test/dummy/app/assets/javascripts/application.js +15 -0
  102. data/test/dummy/app/assets/javascripts/child_resources.js +2 -0
  103. data/test/dummy/app/assets/javascripts/global_resources.js +2 -0
  104. data/test/dummy/app/assets/javascripts/parent_resources.js +2 -0
  105. data/test/dummy/app/assets/javascripts/standalone_resources.js +2 -0
  106. data/test/dummy/app/assets/javascripts/user_groups.js +2 -0
  107. data/test/dummy/app/assets/javascripts/users.js +2 -0
  108. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  109. data/test/dummy/app/assets/stylesheets/child_resources.css +4 -0
  110. data/test/dummy/app/assets/stylesheets/global_resources.css +4 -0
  111. data/test/dummy/app/assets/stylesheets/parent_resources.css +4 -0
  112. data/test/dummy/app/assets/stylesheets/scaffold.css +56 -0
  113. data/test/dummy/app/assets/stylesheets/standalone_resources.css +4 -0
  114. data/test/dummy/app/assets/stylesheets/user_groups.css +4 -0
  115. data/test/dummy/app/assets/stylesheets/users.css +4 -0
  116. data/test/dummy/app/controllers/application_controller.rb +73 -0
  117. data/test/dummy/app/controllers/child_resources_controller.rb +99 -0
  118. data/test/dummy/app/controllers/child_resources_controller.rb~ +83 -0
  119. data/test/dummy/app/controllers/global_resources_controller.rb +95 -0
  120. data/test/dummy/app/controllers/global_resources_controller.rb~ +83 -0
  121. data/test/dummy/app/controllers/parent_resources_controller.rb +101 -0
  122. data/test/dummy/app/controllers/parent_resources_controller.rb~ +83 -0
  123. data/test/dummy/app/controllers/standalone_resources_controller.rb +101 -0
  124. data/test/dummy/app/controllers/standalone_resources_controller.rb~ +83 -0
  125. data/test/dummy/app/controllers/user_groups_controller.rb +131 -0
  126. data/test/dummy/app/controllers/user_sessions_controller.rb +38 -0
  127. data/test/dummy/app/controllers/users_controller.rb +87 -0
  128. data/test/dummy/app/helpers/application_helper.rb +2 -0
  129. data/test/dummy/app/helpers/child_resources_helper.rb +2 -0
  130. data/test/dummy/app/helpers/global_resources_helper.rb +2 -0
  131. data/test/dummy/app/helpers/parent_resources_helper.rb +2 -0
  132. data/test/dummy/app/helpers/standalone_resources_helper.rb +2 -0
  133. data/test/dummy/app/helpers/user_groups_helper.rb +2 -0
  134. data/test/dummy/app/helpers/users_helper.rb +2 -0
  135. data/test/dummy/app/models/child_resource.rb +25 -0
  136. data/test/dummy/app/models/child_resource.rb~ +4 -0
  137. data/test/dummy/app/models/global_resource.rb +3 -0
  138. data/test/dummy/app/models/parent_resource.rb +32 -0
  139. data/test/dummy/app/models/parent_resource.rb~ +3 -0
  140. data/test/dummy/app/models/standalone_resource.rb +22 -0
  141. data/test/dummy/app/models/standalone_resource.rb~ +3 -0
  142. data/test/dummy/app/models/user.rb +80 -0
  143. data/test/dummy/app/models/user_group.rb +12 -0
  144. data/test/dummy/app/views/child_resources/_form.html.erb +30 -0
  145. data/test/dummy/app/views/child_resources/_form.html.erb~ +29 -0
  146. data/test/dummy/app/views/child_resources/edit.html.erb +6 -0
  147. data/test/dummy/app/views/child_resources/index.html.erb +25 -0
  148. data/test/dummy/app/views/child_resources/index.html.erb~ +27 -0
  149. data/test/dummy/app/views/child_resources/new.html.erb +5 -0
  150. data/test/dummy/app/views/child_resources/show.html.erb +20 -0
  151. data/test/dummy/app/views/child_resources/show.html.erb~ +20 -0
  152. data/test/dummy/app/views/global_resources/_form.html.erb +25 -0
  153. data/test/dummy/app/views/global_resources/edit.html.erb +6 -0
  154. data/test/dummy/app/views/global_resources/index.html.erb +25 -0
  155. data/test/dummy/app/views/global_resources/new.html.erb +5 -0
  156. data/test/dummy/app/views/global_resources/show.html.erb +15 -0
  157. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  158. data/test/dummy/app/views/parent_resources/_form.html.erb +25 -0
  159. data/test/dummy/app/views/parent_resources/edit.html.erb +6 -0
  160. data/test/dummy/app/views/parent_resources/index.html.erb +25 -0
  161. data/test/dummy/app/views/parent_resources/index.html.erb~ +25 -0
  162. data/test/dummy/app/views/parent_resources/new.html.erb +5 -0
  163. data/test/dummy/app/views/parent_resources/show.html.erb +44 -0
  164. data/test/dummy/app/views/parent_resources/show.html.erb~ +15 -0
  165. data/test/dummy/app/views/standalone_resources/_form.html.erb +25 -0
  166. data/test/dummy/app/views/standalone_resources/edit.html.erb +6 -0
  167. data/test/dummy/app/views/standalone_resources/index.html.erb +25 -0
  168. data/test/dummy/app/views/standalone_resources/new.html.erb +5 -0
  169. data/test/dummy/app/views/standalone_resources/show.html.erb +15 -0
  170. data/test/dummy/app/views/user_groups/_form.html.haml +9 -0
  171. data/test/dummy/app/views/user_groups/add_members.html.haml +18 -0
  172. data/test/dummy/app/views/user_groups/edit.html.haml +13 -0
  173. data/test/dummy/app/views/user_groups/index.html.haml +20 -0
  174. data/test/dummy/app/views/user_groups/new.html.haml +11 -0
  175. data/test/dummy/app/views/user_groups/show.html.haml +42 -0
  176. data/test/dummy/app/views/user_sessions/new.html.haml +26 -0
  177. data/test/dummy/app/views/users/_form.html.haml +25 -0
  178. data/test/dummy/app/views/users/edit.html.haml +14 -0
  179. data/test/dummy/app/views/users/index.html.haml +26 -0
  180. data/test/dummy/app/views/users/new.html.haml +11 -0
  181. data/test/dummy/app/views/users/show.html.haml +56 -0
  182. data/test/dummy/config.ru +4 -0
  183. data/test/dummy/config/application.rb +59 -0
  184. data/test/dummy/config/boot.rb +10 -0
  185. data/test/dummy/config/database.yml +25 -0
  186. data/test/dummy/config/environment.rb +5 -0
  187. data/test/dummy/config/environments/development.rb +37 -0
  188. data/test/dummy/config/environments/production.rb +67 -0
  189. data/test/dummy/config/environments/test.rb +37 -0
  190. data/test/dummy/config/initializers/alberich.rb +13 -0
  191. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  192. data/test/dummy/config/initializers/inflections.rb +15 -0
  193. data/test/dummy/config/initializers/mime_types.rb +5 -0
  194. data/test/dummy/config/initializers/secret_token.rb +7 -0
  195. data/test/dummy/config/initializers/session_store.rb +8 -0
  196. data/test/dummy/config/initializers/warden.rb +79 -0
  197. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  198. data/test/dummy/config/locales/en.yml +5 -0
  199. data/test/dummy/config/routes.rb +33 -0
  200. data/test/dummy/db/migrate/20120801010101_create_users.rb +20 -0
  201. data/test/dummy/db/migrate/20121121054319_create_user_groups.rb +16 -0
  202. data/test/dummy/db/migrate/20130220160811_create_global_resources.rb +10 -0
  203. data/test/dummy/db/migrate/20130220175258_create_standalone_resources.rb +10 -0
  204. data/test/dummy/db/migrate/20130226145412_create_parent_resources.rb +10 -0
  205. data/test/dummy/db/migrate/20130226151256_create_child_resources.rb +12 -0
  206. data/test/dummy/db/migrate/20130226151256_create_child_resources.rb~ +12 -0
  207. data/test/dummy/db/schema.rb +151 -0
  208. data/test/dummy/db/seeds.rb +65 -0
  209. data/test/dummy/lib/password.rb +58 -0
  210. data/test/dummy/public/404.html +26 -0
  211. data/test/dummy/public/422.html +26 -0
  212. data/test/dummy/public/500.html +25 -0
  213. data/test/dummy/public/favicon.ico +0 -0
  214. data/test/dummy/script/rails +6 -0
  215. data/test/dummy/test/fixtures/child_resources.yml +11 -0
  216. data/test/dummy/test/fixtures/global_resources.yml +9 -0
  217. data/test/dummy/test/fixtures/parent_resources.yml +9 -0
  218. data/test/dummy/test/fixtures/standalone_resources.yml +9 -0
  219. data/test/dummy/test/fixtures/user_groups.yml +9 -0
  220. data/test/dummy/test/fixtures/users.yml +37 -0
  221. data/test/dummy/test/functional/child_resources_controller_test.rb +49 -0
  222. data/test/dummy/test/functional/global_resources_controller_test.rb +49 -0
  223. data/test/dummy/test/functional/parent_resources_controller_test.rb +49 -0
  224. data/test/dummy/test/functional/standalone_resources_controller_test.rb +49 -0
  225. data/test/dummy/test/functional/user_groups_controller_test.rb +49 -0
  226. data/test/dummy/test/functional/users_controller_test.rb +39 -0
  227. data/test/dummy/test/unit/child_resource_test.rb +7 -0
  228. data/test/dummy/test/unit/global_resource_test.rb +7 -0
  229. data/test/dummy/test/unit/helpers/child_resources_helper_test.rb +4 -0
  230. data/test/dummy/test/unit/helpers/global_resources_helper_test.rb +4 -0
  231. data/test/dummy/test/unit/helpers/parent_resources_helper_test.rb +4 -0
  232. data/test/dummy/test/unit/helpers/standalone_resources_helper_test.rb +4 -0
  233. data/test/dummy/test/unit/helpers/user_groups_helper_test.rb +4 -0
  234. data/test/dummy/test/unit/helpers/users_helper_test.rb +4 -0
  235. data/test/dummy/test/unit/parent_resource_test.rb +7 -0
  236. data/test/dummy/test/unit/standalone_resource_test.rb +7 -0
  237. data/test/dummy/test/unit/user_group_test.rb +7 -0
  238. data/test/dummy/test/unit/user_test.rb +7 -0
  239. data/test/integration/alberich/permission_test.rb +7 -0
  240. data/test/integration/alberich/privilege_test.rb +7 -0
  241. data/test/integration/alberich/role_test.rb +7 -0
  242. metadata +639 -0
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ module Alberich
4
+ describe Privilege do
5
+ it "should require unique action for target and role" do
6
+ role1 = FactoryGirl.create(:role)
7
+ role2 = FactoryGirl.create(:role)
8
+ priv1 = FactoryGirl.create(:privilege, :action => "create",
9
+ :target_type => "Alberich::BasePermissionObject",
10
+ :role_id => role1.id)
11
+ priv2 = FactoryGirl.create(:privilege, :action => "create",
12
+ :target_type => "Alberich::BasePermissionObject",
13
+ :role_id => role2.id)
14
+ priv2.role = priv1.role
15
+ priv2.should_not be_valid
16
+ end
17
+ it "should enforce validity of action" do
18
+ role1 = FactoryGirl.create(:role)
19
+ u = FactoryGirl.create(:privilege, :role_id => role1.id)
20
+ u.valid?.should be_true
21
+ u.action = "I'm Invalid"
22
+ u.valid?.should be_false
23
+ u.errors[:action].should_not be_nil
24
+ u.errors[:action][0].should =~ /^is not included in the list.*/
25
+ end
26
+
27
+ it "should enforce validity of target_type" do
28
+ role1 = FactoryGirl.create(:role)
29
+ u = FactoryGirl.create(:privilege, :role_id => role1.id)
30
+ u.valid?.should be_true
31
+ u.target_type = "I'm Invalid"
32
+ u.valid?.should be_false
33
+ u.errors[:target_type].should_not be_nil
34
+ u.errors[:target_type][0].should =~ /^is not included in the list.*/
35
+ end
36
+
37
+
38
+ end
39
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ module Alberich
4
+ describe Role do
5
+ it "should not be valid if name is too long" do
6
+ u = FactoryGirl.create(:role)
7
+ u.name = ('a' * 256)
8
+ u.valid?.should be_false
9
+ u.errors[:name].should_not be_nil
10
+ u.errors[:name][0].should =~ /^is too long.*/
11
+ end
12
+
13
+ it "should enforce validity of scope" do
14
+ u = FactoryGirl.create(:role)
15
+ u.valid?.should be_true
16
+ u.scope = "I'm Invalid"
17
+ u.valid?.should be_false
18
+ u.errors[:scope].should_not be_nil
19
+ u.errors[:scope][0].should =~ /^is not included in the list.*/
20
+ end
21
+
22
+ it "should require unique name" do
23
+ role1 = FactoryGirl.create(:role)
24
+ role2 = FactoryGirl.create(:role)
25
+ role1.should be_valid
26
+ role2.should be_valid
27
+
28
+ role2.name = role1.name
29
+ role2.should_not be_valid
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ module Alberich
4
+ describe SessionEntity do
5
+ it "should require unique entity for user and session" do
6
+ user = FactoryGirl.create(:user)
7
+ group1 = FactoryGirl.create(:user_group)
8
+ group2 = FactoryGirl.create(:user_group)
9
+ session = FactoryGirl.create(:permission_session, :user_id=>user.id)
10
+ entity1 = SessionEntity.new(:permission_session_id => session.id,
11
+ :user_id => user.id,
12
+ :entity_id => Entity.for_target(group1).id)
13
+ entity1.should be_valid
14
+ entity1.save!
15
+ entity2 = SessionEntity.new(:permission_session_id => session.id,
16
+ :user_id => user.id,
17
+ :entity_id => Entity.for_target(group2).id)
18
+ entity2.should be_valid
19
+ entity2.entity = entity1.entity
20
+ entity2.should_not be_valid
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,81 @@
1
+ # Setup Rails Envinronment
2
+ ENV["RAILS_ENV"] = "test"
3
+ require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)
4
+ require 'rspec/rails'
5
+ require 'factory_girl'
6
+
7
+ ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')
8
+
9
+ # Requires supporting ruby files with custom matchers and macros, etc,
10
+ # in spec/support/ and its subdirectories.
11
+
12
+ Dir[File.join(ENGINE_RAILS_ROOT, "spec/support/**/*.rb")].each {|f| require f }
13
+ Dir.glob(File.join(File.dirname(__FILE__) + "/factories/", "**", "*.rb")).each do |file|
14
+ require file
15
+ end
16
+
17
+ RSpec.configure do |config|
18
+ config.color_enabled = true
19
+ config.formatter = 'documentation'
20
+ config.use_transactional_fixtures = true
21
+
22
+ config.include Warden::Test::Helpers, :type => :request
23
+ config.after(:each, :type => :request) do
24
+ Warden.test_reset!
25
+ end
26
+ config.backtrace_clean_patterns = [
27
+ /\/lib\d*\/ruby\//,
28
+ /bin\//,
29
+ #/gems/,
30
+ #/spec\/spec_helper\.rb/,
31
+ /lib\/rspec\/(core|expectations|matchers|mocks)/
32
+ ]
33
+ end
34
+
35
+ # Override to_xml to use underscore rather than dash
36
+ ActiveRecord::Base.class_eval do
37
+ def to_xml(options={})
38
+ options[:dasherize] ||= false
39
+ super({ :root => self.class.name.split("::").last.underscore }.merge(options))
40
+ end
41
+ end
42
+
43
+ module RequestContentTypeHelper
44
+ def accept_all
45
+ @request.env["HTTP_ACCEPT"] = "*/*"
46
+ end
47
+
48
+ def accept_json
49
+ @request.env["HTTP_ACCEPT"] = "application/json"
50
+ end
51
+
52
+ def accept_xml
53
+ @request.env["HTTP_ACCEPT"] = "application/xml"
54
+ end
55
+
56
+ def send_and_accept_xml
57
+ @request.env["HTTP_ACCEPT"] = "application/xml"
58
+ @request.env["CONTENT_TYPE"] = "application/xml"
59
+ end
60
+
61
+ def send_and_accept_json
62
+ @request.env["HTTP_ACCEPT"] = "application/json"
63
+ @request.env["CONTENT_TYPE"] = "application/json"
64
+ end
65
+ end
66
+
67
+ include RequestContentTypeHelper
68
+ def mock_warden(user)
69
+ request.env['warden'] = mock(Warden, :authenticate => user,
70
+ :authenticate! => user,
71
+ :user => user,
72
+ :raw_session => nil)
73
+ @session_id = 'ee73441902cb9445483e498cb05dc398'
74
+ request.session_options[:id] = @session_id
75
+ if user
76
+ @permission_session = Alberich::PermissionSession.create!(:user => user,
77
+ :session_id => @session_id)
78
+ request.session[:permission_session_id] = @permission_session.id
79
+ @permission_session.update_session_entities(user)
80
+ end
81
+ end
@@ -0,0 +1,41 @@
1
+ # Sets up use_route across controller tests, from:
2
+ # http://bit.ly/PU4Wm4 (stackoverflow)
3
+
4
+ module ControllerSetup
5
+ def get(action, parameters = nil, session = nil, flash = nil)
6
+ process_action(action, parameters, session, flash, "GET")
7
+ end
8
+
9
+ # Executes a request simulating POST HTTP method and set/volley the
10
+ # response
11
+ def post(action, parameters = nil, session = nil, flash = nil)
12
+ process_action(action, parameters, session, flash, "POST")
13
+ end
14
+
15
+ # Executes a request simulating PUT HTTP method and set/volley the
16
+ # response
17
+ def put(action, parameters = nil, session = nil, flash = nil)
18
+ process_action(action, parameters, session, flash, "PUT")
19
+ end
20
+
21
+ # Executes a request simulating DELETE HTTP method and set/volley
22
+ # the response
23
+ def delete(action, parameters = nil, session = nil, flash = nil)
24
+ process_action(action, parameters, session, flash, "DELETE")
25
+ end
26
+
27
+ private
28
+
29
+ def process_action(action, parameters = nil, session = nil, flash = nil, method = "GET")
30
+ parameters ||= {}
31
+ process(action, parameters.merge!(:use_route => :alberich), session, flash, method)
32
+ end
33
+ end
34
+
35
+ RSpec.configure do |c|
36
+ # This will include the routing helpers in the specs so that we can
37
+ # use base_image_path and so on to get to the routes.
38
+ c.include Alberich::Engine.routes.url_helpers
39
+ # Always use the correct route for controller tests
40
+ c.include ControllerSetup, :type => :controller
41
+ end
@@ -0,0 +1,261 @@
1
+ == Welcome to Rails
2
+
3
+ Rails is a web-application framework that includes everything needed to create
4
+ database-backed web applications according to the Model-View-Control pattern.
5
+
6
+ This pattern splits the view (also called the presentation) into "dumb"
7
+ templates that are primarily responsible for inserting pre-built data in between
8
+ HTML tags. The model contains the "smart" domain objects (such as Account,
9
+ Product, Person, Post) that holds all the business logic and knows how to
10
+ persist themselves to a database. The controller handles the incoming requests
11
+ (such as Save New Account, Update Product, Show Post) by manipulating the model
12
+ and directing data to the view.
13
+
14
+ In Rails, the model is handled by what's called an object-relational mapping
15
+ layer entitled Active Record. This layer allows you to present the data from
16
+ database rows as objects and embellish these data objects with business logic
17
+ methods. You can read more about Active Record in
18
+ link:files/vendor/rails/activerecord/README.html.
19
+
20
+ The controller and view are handled by the Action Pack, which handles both
21
+ layers by its two parts: Action View and Action Controller. These two layers
22
+ are bundled in a single package due to their heavy interdependence. This is
23
+ unlike the relationship between the Active Record and Action Pack that is much
24
+ more separate. Each of these packages can be used independently outside of
25
+ Rails. You can read more about Action Pack in
26
+ link:files/vendor/rails/actionpack/README.html.
27
+
28
+
29
+ == Getting Started
30
+
31
+ 1. At the command prompt, create a new Rails application:
32
+ <tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
33
+
34
+ 2. Change directory to <tt>myapp</tt> and start the web server:
35
+ <tt>cd myapp; rails server</tt> (run with --help for options)
36
+
37
+ 3. Go to http://localhost:3000/ and you'll see:
38
+ "Welcome aboard: You're riding Ruby on Rails!"
39
+
40
+ 4. Follow the guidelines to start developing your application. You can find
41
+ the following resources handy:
42
+
43
+ * The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
44
+ * Ruby on Rails Tutorial Book: http://www.railstutorial.org/
45
+
46
+
47
+ == Debugging Rails
48
+
49
+ Sometimes your application goes wrong. Fortunately there are a lot of tools that
50
+ will help you debug it and get it back on the rails.
51
+
52
+ First area to check is the application log files. Have "tail -f" commands
53
+ running on the server.log and development.log. Rails will automatically display
54
+ debugging and runtime information to these files. Debugging info will also be
55
+ shown in the browser on requests from 127.0.0.1.
56
+
57
+ You can also log your own messages directly into the log file from your code
58
+ using the Ruby logger class from inside your controllers. Example:
59
+
60
+ class WeblogController < ActionController::Base
61
+ def destroy
62
+ @weblog = Weblog.find(params[:id])
63
+ @weblog.destroy
64
+ logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
65
+ end
66
+ end
67
+
68
+ The result will be a message in your log file along the lines of:
69
+
70
+ Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
71
+
72
+ More information on how to use the logger is at http://www.ruby-doc.org/core/
73
+
74
+ Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
75
+ several books available online as well:
76
+
77
+ * Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
78
+ * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
79
+
80
+ These two books will bring you up to speed on the Ruby language and also on
81
+ programming in general.
82
+
83
+
84
+ == Debugger
85
+
86
+ Debugger support is available through the debugger command when you start your
87
+ Mongrel or WEBrick server with --debugger. This means that you can break out of
88
+ execution at any point in the code, investigate and change the model, and then,
89
+ resume execution! You need to install ruby-debug to run the server in debugging
90
+ mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
91
+
92
+ class WeblogController < ActionController::Base
93
+ def index
94
+ @posts = Post.all
95
+ debugger
96
+ end
97
+ end
98
+
99
+ So the controller will accept the action, run the first line, then present you
100
+ with a IRB prompt in the server window. Here you can do things like:
101
+
102
+ >> @posts.inspect
103
+ => "[#<Post:0x14a6be8
104
+ @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
105
+ #<Post:0x14a6620
106
+ @attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
107
+ >> @posts.first.title = "hello from a debugger"
108
+ => "hello from a debugger"
109
+
110
+ ...and even better, you can examine how your runtime objects actually work:
111
+
112
+ >> f = @posts.first
113
+ => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
114
+ >> f.
115
+ Display all 152 possibilities? (y or n)
116
+
117
+ Finally, when you're ready to resume execution, you can enter "cont".
118
+
119
+
120
+ == Console
121
+
122
+ The console is a Ruby shell, which allows you to interact with your
123
+ application's domain model. Here you'll have all parts of the application
124
+ configured, just like it is when the application is running. You can inspect
125
+ domain models, change values, and save to the database. Starting the script
126
+ without arguments will launch it in the development environment.
127
+
128
+ To start the console, run <tt>rails console</tt> from the application
129
+ directory.
130
+
131
+ Options:
132
+
133
+ * Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
134
+ made to the database.
135
+ * Passing an environment name as an argument will load the corresponding
136
+ environment. Example: <tt>rails console production</tt>.
137
+
138
+ To reload your controllers and models after launching the console run
139
+ <tt>reload!</tt>
140
+
141
+ More information about irb can be found at:
142
+ link:http://www.rubycentral.org/pickaxe/irb.html
143
+
144
+
145
+ == dbconsole
146
+
147
+ You can go to the command line of your database directly through <tt>rails
148
+ dbconsole</tt>. You would be connected to the database with the credentials
149
+ defined in database.yml. Starting the script without arguments will connect you
150
+ to the development database. Passing an argument will connect you to a different
151
+ database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
152
+ PostgreSQL and SQLite 3.
153
+
154
+ == Description of Contents
155
+
156
+ The default directory structure of a generated Ruby on Rails application:
157
+
158
+ |-- app
159
+ | |-- assets
160
+ | |-- images
161
+ | |-- javascripts
162
+ | `-- stylesheets
163
+ | |-- controllers
164
+ | |-- helpers
165
+ | |-- mailers
166
+ | |-- models
167
+ | `-- views
168
+ | `-- layouts
169
+ |-- config
170
+ | |-- environments
171
+ | |-- initializers
172
+ | `-- locales
173
+ |-- db
174
+ |-- doc
175
+ |-- lib
176
+ | `-- tasks
177
+ |-- log
178
+ |-- public
179
+ |-- script
180
+ |-- test
181
+ | |-- fixtures
182
+ | |-- functional
183
+ | |-- integration
184
+ | |-- performance
185
+ | `-- unit
186
+ |-- tmp
187
+ | |-- cache
188
+ | |-- pids
189
+ | |-- sessions
190
+ | `-- sockets
191
+ `-- vendor
192
+ |-- assets
193
+ `-- stylesheets
194
+ `-- plugins
195
+
196
+ app
197
+ Holds all the code that's specific to this particular application.
198
+
199
+ app/assets
200
+ Contains subdirectories for images, stylesheets, and JavaScript files.
201
+
202
+ app/controllers
203
+ Holds controllers that should be named like weblogs_controller.rb for
204
+ automated URL mapping. All controllers should descend from
205
+ ApplicationController which itself descends from ActionController::Base.
206
+
207
+ app/models
208
+ Holds models that should be named like post.rb. Models descend from
209
+ ActiveRecord::Base by default.
210
+
211
+ app/views
212
+ Holds the template files for the view that should be named like
213
+ weblogs/index.html.erb for the WeblogsController#index action. All views use
214
+ eRuby syntax by default.
215
+
216
+ app/views/layouts
217
+ Holds the template files for layouts to be used with views. This models the
218
+ common header/footer method of wrapping views. In your views, define a layout
219
+ using the <tt>layout :default</tt> and create a file named default.html.erb.
220
+ Inside default.html.erb, call <% yield %> to render the view using this
221
+ layout.
222
+
223
+ app/helpers
224
+ Holds view helpers that should be named like weblogs_helper.rb. These are
225
+ generated for you automatically when using generators for controllers.
226
+ Helpers can be used to wrap functionality for your views into methods.
227
+
228
+ config
229
+ Configuration files for the Rails environment, the routing map, the database,
230
+ and other dependencies.
231
+
232
+ db
233
+ Contains the database schema in schema.rb. db/migrate contains all the
234
+ sequence of Migrations for your schema.
235
+
236
+ doc
237
+ This directory is where your application documentation will be stored when
238
+ generated using <tt>rake doc:app</tt>
239
+
240
+ lib
241
+ Application specific libraries. Basically, any kind of custom code that
242
+ doesn't belong under controllers, models, or helpers. This directory is in
243
+ the load path.
244
+
245
+ public
246
+ The directory available for the web server. Also contains the dispatchers and the
247
+ default HTML files. This should be set as the DOCUMENT_ROOT of your web
248
+ server.
249
+
250
+ script
251
+ Helper scripts for automation and generation.
252
+
253
+ test
254
+ Unit and functional tests along with fixtures. When using the rails generate
255
+ command, template test files will be generated for you and placed in this
256
+ directory.
257
+
258
+ vendor
259
+ External libraries that the application depends on. Also includes the plugins
260
+ subdirectory. If the app has frozen rails, those gems also go here, under
261
+ vendor/rails/. This directory is in the load path.