troles 0.5.2 → 0.6.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 (54) hide show
  1. data/Gemfile +17 -12
  2. data/Gemfile.lock +42 -38
  3. data/README.textile +29 -0
  4. data/Rakefile +1 -23
  5. data/VERSION +1 -1
  6. data/lib/trole.rb +1 -5
  7. data/lib/trole_groups/macros.rb +1 -2
  8. data/lib/trole_groups.rb +4 -7
  9. data/lib/troles/adapters/active_record/config.rb +28 -39
  10. data/lib/troles/common/api/read.rb +1 -0
  11. data/lib/troles/common/config/class_methods.rb +44 -0
  12. data/lib/troles/common/config/schema/helpers.rb +0 -79
  13. data/lib/troles/common/config/schema.rb +40 -37
  14. data/lib/troles/common/config/valid_roles.rb +13 -1
  15. data/lib/troles/common/config.rb +46 -79
  16. data/lib/troles/common/dependencies.rb +1 -0
  17. data/lib/troles/common/macros/configuration/base_loader.rb +6 -10
  18. data/lib/troles/common/macros/configuration/config_loader.rb +2 -7
  19. data/lib/troles/common/macros/configuration/storage_loader.rb +3 -8
  20. data/lib/troles/common/macros/configuration/strategy_loader.rb +3 -17
  21. data/lib/troles/common/macros/configuration.rb +2 -5
  22. data/lib/troles/common/macros.rb +2 -3
  23. data/lib/troles/common/marshaller.rb +1 -2
  24. data/lib/troles/common/operations.rb +1 -2
  25. data/lib/troles/common.rb +1 -6
  26. data/lib/troles/config.rb +0 -8
  27. data/lib/troles/macros.rb +0 -2
  28. data/lib/troles/storage/base_many.rb +1 -1
  29. data/lib/troles/storage.rb +1 -6
  30. data/lib/troles/strategy.rb +2 -0
  31. data/lib/troles.rb +3 -2
  32. data/spec/active_record/migrations/many/bit_many.rb +1 -7
  33. data/spec/active_record/migrations/many/custom_join.rb +2 -9
  34. data/spec/active_record/migrations/many/join_ref_many.rb +1 -10
  35. data/spec/active_record/migrations/many/ref_many.rb +7 -8
  36. data/spec/active_record/migrations/many/string_many.rb +1 -7
  37. data/spec/active_record/migrations/one/bit_one.rb +1 -5
  38. data/spec/active_record/migrations/one/ref_one.rb +1 -6
  39. data/spec/active_record/migrations/one/string_one.rb +1 -5
  40. data/spec/active_record_helper.rb +8 -4
  41. data/spec/factories.rb +2 -0
  42. data/spec/trole_groups/api/read_api_spec.rb +1 -1
  43. data/spec/trole_groups/strategies/{ref_many.rb → ref_many_spec.rb} +0 -0
  44. data/spec/troles/common/config/schema_spec.rb +5 -0
  45. data/spec/troles/common/multi_roles_spec.rb +2 -2
  46. data/spec/troles/marshaller/bitmask_spec.rb +11 -5
  47. data/spec/troles/marshaller/generic_spec.rb +17 -0
  48. data/spec/troles/storage/bit_many_spec.rb +20 -0
  49. data/spec/troles/storage/ref_many_spec.rb +20 -0
  50. data/spec/troles/storage/string_many_spec.rb +19 -0
  51. data/spec/troles/strategies/string_many_spec.rb +1 -1
  52. data/troles.gemspec +24 -43
  53. metadata +39 -122
  54. data/lib/troles/common/config/schema/role_helpers.rb +0 -27
data/Gemfile CHANGED
@@ -1,28 +1,33 @@
1
1
  source :rubygems
2
2
 
3
- gem 'sugar-high', '>= 0.4.4'
4
- gem 'sweetloader', '>= 0.1.0'
5
- gem 'require_all', '~> 1.2.0'
3
+ gem 'sugar-high', '>= 0.6.0'
4
+ gem 'sweetloader', '>= 0.1.1'
5
+ gem 'schemaker', '>= 0.1.4'
6
6
 
7
- group :development, :test do
8
- gem 'yard', '> 0.6.1'
7
+ group :test do
9
8
  gem 'rails', '>= 3.0.1'
10
9
  gem 'rspec-rails', '>= 2.4.0'
11
- gem 'sqlite3'
10
+
12
11
  gem 'capybara', '> 0.4.0'
13
12
  gem 'shoulda'
14
13
  gem 'factory_girl_rails'
15
14
  gem 'mocha'
16
- gem 'database_cleaner', '~> 0.6.8', :git => "git://github.com/kristianmandrup/database_cleaner.git"
15
+ gem 'database_cleaner', '~> 0.6.8', :git =>"git://github.com/kristianmandrup/database_cleaner.git"
16
+
17
+ gem 'require_all', '~> 1.2.0'
18
+ gem 'sqlite3'
19
+
20
+ # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
21
+ # gem 'ruby-debug'
22
+ # gem 'ruby-debug19'
23
+ end
17
24
 
18
- gem 'rake', '~> 0.9.0'
25
+ group :development do
26
+ gem 'rake', '>= 0.9.0'
27
+ gem 'rspec', '>= 2.4'
19
28
  gem 'bundler', '>= 1'
20
29
  gem 'jeweler', '>= 1.6'
21
30
  gem 'rdoc', '>= 3.6'
22
31
  gem 'rcov'
23
32
  gem 'yard', '> 0.6.1'
24
-
25
- # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
26
- # gem 'ruby-debug'
27
- # gem 'ruby-debug19'
28
33
  end
data/Gemfile.lock CHANGED
@@ -7,37 +7,35 @@ GIT
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
- actionmailer (3.1.0)
11
- actionpack (= 3.1.0)
10
+ actionmailer (3.1.1)
11
+ actionpack (= 3.1.1)
12
12
  mail (~> 2.3.0)
13
- actionpack (3.1.0)
14
- activemodel (= 3.1.0)
15
- activesupport (= 3.1.0)
13
+ actionpack (3.1.1)
14
+ activemodel (= 3.1.1)
15
+ activesupport (= 3.1.1)
16
16
  builder (~> 3.0.0)
17
17
  erubis (~> 2.7.0)
18
18
  i18n (~> 0.6)
19
19
  rack (~> 1.3.2)
20
- rack-cache (~> 1.0.3)
20
+ rack-cache (~> 1.1)
21
21
  rack-mount (~> 0.8.2)
22
22
  rack-test (~> 0.6.1)
23
- sprockets (~> 2.0.0)
24
- activemodel (3.1.0)
25
- activesupport (= 3.1.0)
26
- bcrypt-ruby (~> 3.0.0)
23
+ sprockets (~> 2.0.2)
24
+ activemodel (3.1.1)
25
+ activesupport (= 3.1.1)
27
26
  builder (~> 3.0.0)
28
27
  i18n (~> 0.6)
29
- activerecord (3.1.0)
30
- activemodel (= 3.1.0)
31
- activesupport (= 3.1.0)
28
+ activerecord (3.1.1)
29
+ activemodel (= 3.1.1)
30
+ activesupport (= 3.1.1)
32
31
  arel (~> 2.2.1)
33
32
  tzinfo (~> 0.3.29)
34
- activeresource (3.1.0)
35
- activemodel (= 3.1.0)
36
- activesupport (= 3.1.0)
37
- activesupport (3.1.0)
33
+ activeresource (3.1.1)
34
+ activemodel (= 3.1.1)
35
+ activesupport (= 3.1.1)
36
+ activesupport (3.1.1)
38
37
  multi_json (~> 1.0)
39
38
  arel (2.2.1)
40
- bcrypt-ruby (3.0.1)
41
39
  builder (3.0.0)
42
40
  capybara (1.1.1)
43
41
  mime-types (>= 1.16)
@@ -63,6 +61,7 @@ GEM
63
61
  bundler (~> 1.0)
64
62
  git (>= 1.2.5)
65
63
  rake
64
+ json (1.6.1)
66
65
  json_pure (1.6.1)
67
66
  mail (2.3.0)
68
67
  i18n (>= 0.4.0)
@@ -75,8 +74,8 @@ GEM
75
74
  multi_json (1.0.3)
76
75
  nokogiri (1.5.0)
77
76
  polyglot (0.3.2)
78
- rack (1.3.3)
79
- rack-cache (1.0.3)
77
+ rack (1.3.4)
78
+ rack-cache (1.1)
80
79
  rack (>= 0.4)
81
80
  rack-mount (0.8.3)
82
81
  rack (>= 1.0.0)
@@ -84,24 +83,25 @@ GEM
84
83
  rack
85
84
  rack-test (0.6.1)
86
85
  rack (>= 1.0)
87
- rails (3.1.0)
88
- actionmailer (= 3.1.0)
89
- actionpack (= 3.1.0)
90
- activerecord (= 3.1.0)
91
- activeresource (= 3.1.0)
92
- activesupport (= 3.1.0)
86
+ rails (3.1.1)
87
+ actionmailer (= 3.1.1)
88
+ actionpack (= 3.1.1)
89
+ activerecord (= 3.1.1)
90
+ activeresource (= 3.1.1)
91
+ activesupport (= 3.1.1)
93
92
  bundler (~> 1.0)
94
- railties (= 3.1.0)
95
- railties (3.1.0)
96
- actionpack (= 3.1.0)
97
- activesupport (= 3.1.0)
93
+ railties (= 3.1.1)
94
+ railties (3.1.1)
95
+ actionpack (= 3.1.1)
96
+ activesupport (= 3.1.1)
98
97
  rack-ssl (~> 1.3.2)
99
98
  rake (>= 0.8.7)
100
99
  rdoc (~> 3.4)
101
100
  thor (~> 0.14.6)
102
101
  rake (0.9.2)
103
- rcov (0.9.10)
104
- rdoc (3.9.4)
102
+ rcov (0.9.11)
103
+ rdoc (3.10)
104
+ json (~> 1.4)
105
105
  require_all (1.2.0)
106
106
  rspec (2.6.0)
107
107
  rspec-core (~> 2.6.0)
@@ -117,20 +117,22 @@ GEM
117
117
  railties (~> 3.0)
118
118
  rspec (~> 2.6.0)
119
119
  rubyzip (0.9.4)
120
- selenium-webdriver (2.7.0)
120
+ schemaker (0.1.4)
121
+ sweetloader (>= 0.1.1)
122
+ selenium-webdriver (2.8.0)
121
123
  childprocess (>= 0.2.1)
122
124
  ffi (>= 1.0.7)
123
125
  json_pure
124
126
  rubyzip
125
127
  shoulda (2.11.3)
126
- sprockets (2.0.1)
128
+ sprockets (2.0.2)
127
129
  hike (~> 1.2)
128
130
  rack (~> 1.0)
129
131
  tilt (!= 1.3.0, ~> 1.1)
130
132
  sqlite3 (1.3.4)
131
133
  sugar-high (0.6.0)
132
134
  activesupport (>= 3.0.1)
133
- sweetloader (0.1.0)
135
+ sweetloader (0.1.1)
134
136
  activesupport (>= 3.0.1)
135
137
  i18n
136
138
  thor (0.14.6)
@@ -154,13 +156,15 @@ DEPENDENCIES
154
156
  jeweler (>= 1.6)
155
157
  mocha
156
158
  rails (>= 3.0.1)
157
- rake (~> 0.9.0)
159
+ rake (>= 0.9.0)
158
160
  rcov
159
161
  rdoc (>= 3.6)
160
162
  require_all (~> 1.2.0)
163
+ rspec (>= 2.4)
161
164
  rspec-rails (>= 2.4.0)
165
+ schemaker (>= 0.1.4)
162
166
  shoulda
163
167
  sqlite3
164
- sugar-high (>= 0.4.4)
165
- sweetloader (>= 0.1.0)
168
+ sugar-high (>= 0.6.0)
169
+ sweetloader (>= 0.1.1)
166
170
  yard (> 0.6.1)
data/README.textile CHANGED
@@ -13,6 +13,35 @@ The roles list cache of a role subject (fx a user) is only updated (retrieved fr
13
13
 
14
14
  Note: Troles is a full redesign of _roles generic_ and company, using lessons learned. Troles uses a much cleaner design. It is aimed at being easy to extend and easy to create adapters for etc.
15
15
 
16
+ h2. Update: Oct. 12
17
+
18
+ Updated to use the latest _sugar-high_, _sweetloader_ and _schemaker_ gems.
19
+ Fixed most specs so they pass again.
20
+
21
+ In general, troles should now be working with respect to the API and various implementation strategies.
22
+
23
+ h3. Yard documentation
24
+
25
+ This project uses "Yard":http://rubydoc.info/docs/yard/file/docs/GettingStarted.md for documentation.
26
+
27
+ <pre>$ yard server
28
+
29
+ From browser, go to: http://0.0.0.0:8808/ # then you can enjoy! the nice documentation :)
30
+ </pre>
31
+
32
+ There is now support for Caching and invalidation of the _role_list_ when the roles change.
33
+ The specs now validate that caching works as it should.
34
+
35
+ Please help out to finalize this project! :)
36
+
37
+ h2. Bug hunting by running specs
38
+
39
+ Run specs for at most one strategy at the time for now...
40
+
41
+ @$ rspec spec/troles/strategies/bit_many_spec.rb@
42
+
43
+ Please see the document _spec/Guide to running specs.textile_ where I advise on how best to do "bug hunting" to help get this project off the ground!
44
+
16
45
  h2. Role strategies
17
46
 
18
47
  The following lists the role strategies to be supported
data/Rakefile CHANGED
@@ -10,6 +10,7 @@ rescue Bundler::BundlerError => e
10
10
  exit e.status_code
11
11
  end
12
12
  require 'rake'
13
+ require 'rspec'
13
14
 
14
15
  require 'jeweler'
15
16
  Jeweler::Tasks.new do |gem|
@@ -23,26 +24,3 @@ Jeweler::Tasks.new do |gem|
23
24
  gem.authors = ["Kristian Mandrup"]
24
25
  end
25
26
  Jeweler::RubygemsDotOrgTasks.new
26
-
27
- require 'rspec/core'
28
- require 'rspec/core/rake_task'
29
- RSpec::Core::RakeTask.new(:spec) do |spec|
30
- spec.pattern = FileList['spec/**/*_spec.rb']
31
- end
32
-
33
- RSpec::Core::RakeTask.new(:rcov) do |spec|
34
- spec.pattern = 'spec/**/*_spec.rb'
35
- spec.rcov = true
36
- end
37
-
38
- task :default => :spec
39
-
40
- require 'rake/rdoctask'
41
- Rake::RDocTask.new do |rdoc|
42
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
43
-
44
- rdoc.rdoc_dir = 'rdoc'
45
- rdoc.title = "mongoid_geo #{version}"
46
- rdoc.rdoc_files.include('README*')
47
- rdoc.rdoc_files.include('lib/**/*.rb')
48
- end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.2
1
+ 0.6.0
data/lib/trole.rb CHANGED
@@ -2,9 +2,5 @@
2
2
  require 'troles/common'
3
3
 
4
4
  module Trole
5
- autoload :Config, 'trole/config'
6
- autoload :Api, 'trole/api'
7
- autoload :Operations, 'trole/operations'
8
- autoload :Strategy, 'trole/strategy'
9
- autoload :Storage, 'trole/storage'
5
+ autoload_modules :Config, :Api, :Operations, :Strategy, :Storage
10
6
  end
@@ -13,7 +13,7 @@
13
13
 
14
14
  module TroleGroups
15
15
  module Macros
16
- autoload :Configuration, 'trole_groups/macros/configuration'
16
+ autoload_modules :Configuration
17
17
 
18
18
  def trolegroups_strategy strategy, options = {}, &block
19
19
  configuration = Configuration.new self, strategy, options
@@ -39,4 +39,3 @@ module TroleGroups
39
39
  end
40
40
  end
41
41
 
42
- Module.send :include, TroleGroups::Macros
data/lib/trole_groups.rb CHANGED
@@ -1,10 +1,7 @@
1
1
  require 'troles/common'
2
- require 'troles/macros'
3
2
 
4
3
  module TroleGroups
5
- autoload :Config, 'trole_groups/config'
6
- autoload :Api, 'trole_groups/api'
7
- autoload :Operations, 'trole_groups/operations'
8
- autoload :Strategy, 'trole_groups/strategy'
9
- autoload :Storage, 'trole_groups/storage'
10
- end
4
+ autoload_modules :Config, :Api, :Operations, :Strategy, :Storage, :Macros
5
+ end
6
+
7
+ Module.send :include, TroleGroups::Macros
@@ -1,8 +1,12 @@
1
1
  module Troles::ActiveRecord
2
2
  class Config < Troles::Common::Config
3
+
4
+ attr_reader :models
3
5
 
4
6
  def initialize subject_class, options = {}
5
- super
7
+ super
8
+ puts "models classes: #{subject_class}, #{object_model}, #{join_model}"
9
+ @models = ::Schemaker::Models.new(subject_class, object_model, join_model)
6
10
  end
7
11
 
8
12
  def configure_relation
@@ -10,8 +14,8 @@ module Troles::ActiveRecord
10
14
  when :join_ref_many
11
15
  configure_join_model
12
16
  when :ref_many
13
- return configure_join_model if join_model
14
- has_and_belongs_many subject_class, object_model, :key => :accounts
17
+ return configure_join_model if join_model?
18
+ subject.quick_join
15
19
  when :embed_many
16
20
  raise "Embed many configuration not yet implemented for ActiveRecord"
17
21
  end
@@ -23,38 +27,37 @@ module Troles::ActiveRecord
23
27
 
24
28
  protected
25
29
 
26
- def object_model
27
- role_model
28
- end
29
-
30
- def join_model
31
- role_join_model
30
+ def subject
31
+ @subject ||= models.subject_model
32
32
  end
33
33
 
34
+ def main_field
35
+ role_field
36
+ end
34
37
 
35
- def role_join_model
38
+ def join_model
36
39
  @join_model_found ||= begin
37
- models = [@join_model, join_model_best_guess].select do |class_name|
38
- try_class(class_name.to_s.camelize)
39
- end.compact
40
- # puts "role models found: #{models}"
41
- raise "No #{subject_class} to #{object_model} join class defined, define a #{join_model_best_guess} model class or set which class to use, using the :role_join_model option on configuration" if models.empty?
42
- models.first.to_s.constantize
40
+ find_first_class(@join_model, join_model_best_guess)
43
41
  end
42
+ rescue ClassExt::ClassNotFoundError
43
+ nil
44
+ end
45
+
46
+ def join_model?
47
+ join_model
44
48
  end
45
49
 
46
50
  def join_model_best_guess
47
51
  "#{subject_class.to_s.pluralize}#{object_model.to_s.pluralize}"
48
52
  end
49
53
 
50
- def role_join_model= model_class
54
+ def join_model= model_class
51
55
  @join_model = model_class and return if model_class.any_kind_of?(Class, String, Symbol)
52
- raise "The role model must be a Class, was: #{model_class}"
56
+ raise "The join model must be a Class, was: #{model_class}"
53
57
  end
54
58
 
55
-
56
59
  def join_key
57
- make_key role_join_model
60
+ make_key join_model
58
61
  end
59
62
 
60
63
  def configure_join_model
@@ -64,25 +67,11 @@ module Troles::ActiveRecord
64
67
  puts "Role class: #{object_model}"
65
68
  puts "Join class: #{join_model}"
66
69
  end
67
-
68
- # UserAccount
69
- # has_many :troles, :class_name => 'Role', :through => :users_roles
70
- has_many_for subject_class, object_model, :opts => {:through => join_key.to_sym}
71
- # has_many :user_roles, :class_name => 'UserRole'
72
- has_many_for subject_class, role_join_model, :key => join_key
73
-
74
- # UserRole (custom join class name)
75
- # belongs_to :user, :class_name => 'UserAccount'
76
- belongs_to_for role_join_model, subject_class
77
- # belongs_to :role, :class_name => 'Role'
78
- belongs_to_for role_join_model, role_model
79
-
80
- # Role
81
- # has_many :accounts, :class_name => 'User', :through => :user_roles
82
- has_many_for object_model, subject_class, :key => :accounts, :opts => {:through => join_key.to_sym}
83
-
84
- # has_many :user_roles, :class_name => 'UserRole'
85
- has_many_for object_model, role_join_model, :key => join_key
70
+
71
+ [:object, :subject, :join].each do |type|
72
+ clazz = "Schemaker::#{type.to_s.camelize}Model".constantize
73
+ clazz.new(model).configure
74
+ end
86
75
  end
87
76
  end
88
77
  end
@@ -34,6 +34,7 @@ module Troles::Common::Api
34
34
  def has_roles? *roles
35
35
  (roles.to_symbols - role_list).empty?
36
36
  end
37
+ alias_method :has_all_roles?, :has_roles?
37
38
 
38
39
  # Checks if the role subject has any of the listed roles
39
40
  # (see #has_roles?)
@@ -0,0 +1,44 @@
1
+ module Troles::Common
2
+ class Config
3
+ module ClassMethods
4
+ attr_reader :default_orm, :auto_load
5
+ attr_accessor :log_on
6
+
7
+ def log_on?
8
+ log_on || false
9
+ end
10
+
11
+ def default_orm= orm
12
+ @default_orm ||= orm
13
+ end
14
+
15
+ def auto_load= mode
16
+ raise "Autoload must be set to true or false" if ![true, false].include? mode
17
+ @auto_load = mode
18
+ end
19
+
20
+ def auto_load?
21
+ @auto_load
22
+ end
23
+
24
+ def auto_config
25
+ auto_config_setings
26
+ end
27
+
28
+ def auto_config? name
29
+ auto_config_setings[name]
30
+ end
31
+
32
+ protected
33
+
34
+ def auto_config_setings
35
+ @auto_config_setings ||= auto_config_defaults
36
+ end
37
+
38
+ # default auto_config settings
39
+ def auto_config_defaults
40
+ {:models => true, :relations => true, :fields => true}
41
+ end
42
+ end
43
+ end
44
+ end
@@ -11,85 +11,6 @@ module Troles::Common
11
11
  def boolean? value
12
12
  [true, false].include? value
13
13
  end
14
-
15
- # TODO: Needs extraction into helper module!
16
-
17
- def belongs_to_for from, to, options = {}
18
- make_relationship :belongs_to, from, to, options
19
- end
20
-
21
- def has_many_for from, to, options = {}
22
- make_relationship :has_many, from, to, options
23
- end
24
-
25
- def has_one_for from, to, options = {}
26
- make_relationship :has_one, from, to, options
27
- end
28
-
29
- # To setup sth like this:
30
- #
31
- # class UserAccount < ActiveRecord::Base
32
- # has_and_belongs_to_many :troles, :class_name => 'Role'
33
- # end
34
- #
35
- # class Role < ActiveRecord::Base
36
- # has_and_belongs_to_many :user_accounts, :class_name => 'User'
37
- # end
38
- def has_and_belongs_many from, to, options = {}
39
- make_relationship :has_and_belongs_to_many, from, to, :key => role_field
40
- make_relationship :has_and_belongs_to_many, to, from, options
41
- end
42
-
43
- def get_model_type class_name
44
- return :subject if class_name == subject_class
45
- return :object if class_name == object_model
46
- return :join if class_name == role_join_model
47
- raise "Not a known model: #{class_name}"
48
- end
49
-
50
- # options:
51
- # - :opts, extras options, fx to set the :through relationship
52
- # - :key (usually to enforce use of role_field as key name)
53
- def make_relationship type, from, to, options = {}
54
- # puts "type: #{type}, #{from}, #{to}"
55
- from_type = get_model_type from
56
- to_type = get_model_type to
57
-
58
- key_opts = key_options(type, to_type)
59
-
60
- model_key = options[:key] ? options[:key] : send("#{to_type}_key", key_opts)
61
-
62
- class_name = send "#{to_type}_class_name"
63
-
64
- rel_options = {:class_name => class_name}
65
-
66
- rel_options.merge!(options[:opts]) if options[:opts]
67
-
68
- puts "#{from}.#{type} :#{model_key}, #{rel_options.inspect}" if log_on?
69
-
70
- from.send(type, model_key, rel_options)
71
- end
72
-
73
- def object_key options = {}
74
- make_key "#{object_class_name}", options
75
- end
76
-
77
- def key_options type, obj
78
- return {:prefix => 't'} if (type == :has_many && obj == :object)
79
- {}
80
- end
81
-
82
- def make_key name, options = {}
83
- name = name.to_s.gsub(/::/, '__').underscore.pluralize
84
- parts = name.split('_')
85
- name = parts.inject([]) do |res, part|
86
- res << (part != parts.last ? part.singularize : part)
87
- res
88
- end.join('_')
89
-
90
- key = name
91
- options[:prefix] ? "t#{key}" : key
92
- end
93
14
  end
94
15
  end
95
16
  end
@@ -1,66 +1,69 @@
1
+ #
2
+ # @author Kristian Mandrup
3
+ #
4
+ # Schema configuration
5
+ # Has responsibiloity to configure relationships between models
6
+ #
1
7
  module Troles::Common
2
8
  class Config
3
9
  module Schema
4
- autoload :Helpers, 'troles/common/config/schema/helpers'
5
- autoload :RoleHelpers, 'troles/common/config/schema/role_helpers'
6
-
10
+ autoload_modules :Helpers
11
+
12
+ # configures the models if set to do so
13
+ # first configures
7
14
  def configure_models
8
- configure_generic
15
+ configure_generic if generic?
9
16
  configure_field if auto_config?(:fields)
10
17
  configure_relation if auto_config?(:relations)
11
18
  end
12
19
 
13
- def configure_generic
14
- subject_class.send(:attr_accessor, role_field) if generic? || orm == :generic # create troles accessor
20
+ # configures main field (fx role field) as a generic accessor
21
+ def configure_generic
22
+ raise "Subject class must be a Class" unless subject_class.is_a?(Class)
23
+ subject_class.send(:attr_accessor, main_field) # create troles accessor
15
24
  end
16
25
 
17
- # Adapter should customize this as needed
26
+ # configures a single main field on the subject
27
+ # fx if the roles are stored as a Boolean, Integer or String
28
+ # @note adapter should customize this as needed
18
29
  def configure_field
19
30
  end
20
31
 
21
- # Adapter should customize this as needed
32
+ # configures model relationships for roles
33
+ # fx if the roles are stored in an model, either embedded or referenced
34
+ # @note adapter should customize this as needed
22
35
  def configure_relation
23
36
  end
24
37
 
25
- # Sets the role model to use
26
- # allows different role subject classes (fx User Accounts) to have different role schemas
27
- # @param [Class] the model class
28
- def role_model= model_class
29
- @role_model = model_class.to_s and return if model_class.any_kind_of?(Class, String, Symbol)
30
- raise "The role model must be a Class, was: #{model_class}"
38
+ # Sets the object model to use
39
+ # allows different subject classes (fx User Accounts) to have different schemas
40
+ # @param [Class, String, Symbol] the model class
41
+ def object_model= model_class
42
+ @object_model = model_class.to_s and return if model_class.any_kind_of?(Class, String, Symbol)
43
+ raise "The object model must be a Class, was: #{model_class}"
31
44
  end
45
+ alias_method :role_mode=, :object_model=
32
46
 
33
- # Gets the role model to be used
34
- # see (#role_model=)
35
- # @return [Class] the model class (defaults to Role)
36
- def role_model
37
- @role_model_found ||= begin
38
- models = [@role_model, role_class_name].select do |class_name|
39
- try_class(class_name.to_s.camelize)
40
- end.compact
41
- # puts "role models found: #{models}"
42
- raise "No #{role_class_name} class defined, define a #{role_class_name} class or set which class to use, using the :role_model option on configuration" if models.empty?
43
- models.first.to_s.constantize
44
- end
47
+ # Gets the object model to be used
48
+ # see (#object_model=)
49
+ # see(#default_object_model_class_name)
50
+ #
51
+ # @return [Class] the model class
52
+ def object_model
53
+ @object_model_found ||= find_first_class(@object_model, default_object_model_class_name)
45
54
  end
55
+ alias_method :role_model, :object_model
46
56
 
47
57
  protected
48
58
 
49
- def role_class_name
59
+ def default_object_model_class_name
50
60
  'Role'
51
61
  end
52
62
 
53
- def try_class clazz
54
- begin
55
- clazz = clazz.constantize if clazz.kind_of?(String)
56
- clazz
57
- rescue
58
- false
59
- end
60
- end
63
+ # from sugar-high gem
64
+ include ClassExt
61
65
 
62
- include Helpers
63
- include RoleHelpers
66
+ include Helpers # ??
64
67
  end
65
68
  end
66
69
  end