roles_mongoid 0.3.3 → 0.3.4

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.3
1
+ 0.3.4
@@ -5,6 +5,8 @@ module Mongoid
5
5
  module Generators
6
6
  class RolesGenerator < Rails::Generators::NamedBase
7
7
  desc "Add role strategy to a model"
8
+
9
+ # argument name
8
10
 
9
11
  class_option :strategy, :type => :string, :aliases => "-s", :default => 'role_string',
10
12
  :desc => "Role strategy to use (admin_flag, role_string, roles_string, role_strings, one_role, many_roles, roles_mask)"
@@ -15,9 +17,16 @@ module Mongoid
15
17
  def apply_role_strategy
16
18
  logger.add_logfile :logfile => logfile if logfile
17
19
  logger.debug "apply_role_strategy for : #{strategy} in model #{name}"
18
- insert_into_model name do
20
+
21
+ say "User model #{user_model_name} not found", :red if !has_model_file?(user_model_name)
22
+
23
+ insert_into_model user_model_name, :after => /include Mongoid::\w+/ do
19
24
  insertion_text
20
- end
25
+ end
26
+
27
+ unless read_model(:user) =~ /use_roles_strategy/
28
+ inject_into_file model_file(:user), "use_roles_strategy :#{strategy}\n\n", :before => "class"
29
+ end
21
30
  end
22
31
 
23
32
  protected
@@ -27,6 +36,14 @@ module Mongoid
27
36
 
28
37
  use_orm :mongoid
29
38
 
39
+ def logfile
40
+ options[:logfile]
41
+ end
42
+
43
+ def user_model_name
44
+ name || 'User'
45
+ end
46
+
30
47
  def orm
31
48
  :mongoid
32
49
  end
@@ -52,9 +69,14 @@ module Mongoid
52
69
  end
53
70
 
54
71
  def roles_statement
72
+ return '' if has_valid_roles_statement?
55
73
  roles ? "valid_roles_are #{roles.join(', ')}" : ''
56
74
  end
57
75
 
76
+ def has_valid_roles_statement?
77
+ !(read_model(user_model_name) =~ /valid_roles_are/).nil?
78
+ end
79
+
58
80
  def insertion_text
59
81
  %Q{include Roles::#{orm.to_s.camelize}
60
82
  #{role_strategy_statement}
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{roles_mongoid}
8
- s.version = "0.3.3"
8
+ s.version = "0.3.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
@@ -23,6 +23,7 @@ describe 'role strategy generator: admin_flag' do
23
23
  create_model :user do
24
24
  '# content'
25
25
  end
26
+
26
27
  with_generator do |g|
27
28
  arguments = "User --strategy admin_flag --roles admin user"
28
29
  puts "arguments: #{arguments}"
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 3
9
- version: 0.3.3
8
+ - 4
9
+ version: 0.3.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup