ixtlan-guard 0.7.0 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/features/generators.feature +0 -10
  2. data/features/step_definitions/simple_steps.rb +1 -82
  3. data/lib/ixtlan/guard/guard_ng.rb +77 -76
  4. data/lib/ixtlan/guard/guard_rails.rb +8 -8
  5. data/lib/ixtlan/guard/railtie.rb +1 -1
  6. data/spec/guard_cache_spec.rb +1 -1
  7. data/spec/guard_export_spec.rb +118 -90
  8. data/spec/guard_spec.rb +1 -16
  9. data/spec/guard_with_associations_spec.rb +114 -0
  10. data/spec/guard_with_associations_spec.rb~ +106 -0
  11. data/spec/guards/allow_all_defaults_guard.yml +1 -1
  12. data/spec/guards/defaults_guard.yml +1 -1
  13. data/spec/guards/no_defaults_guard.yml +1 -1
  14. data/spec/guards/only_defaults_guard.yml +1 -1
  15. data/spec/guards/regions_guard.yml +8 -0
  16. data/spec/guards/regions_guard.yml~ +2 -0
  17. data/spec/guards/users_guard.yml +1 -1
  18. metadata +8 -24
  19. data/lib/generators/active_record/templates/flavor_migration.rb +0 -13
  20. data/lib/generators/active_record/templates/flavor_model.rb +0 -8
  21. data/lib/generators/active_record/templates/group_model.rb +0 -43
  22. data/lib/generators/active_record/templates/group_user_migration.rb +0 -13
  23. data/lib/generators/active_record/templates/user_model.rb +0 -124
  24. data/lib/generators/active_record/user_management_models_generator.rb +0 -202
  25. data/lib/generators/erb/user_management_controller_generator.rb +0 -10
  26. data/lib/generators/ixtlan/maintenance_scaffold/USAGE +0 -8
  27. data/lib/generators/ixtlan/maintenance_scaffold/maintenance_scaffold_generator.rb +0 -40
  28. data/lib/generators/ixtlan/permissions_scaffold/USAGE +0 -8
  29. data/lib/generators/ixtlan/permissions_scaffold/permissions_scaffold_generator.rb +0 -33
  30. data/lib/generators/ixtlan/user_management_controller/USAGE +0 -8
  31. data/lib/generators/ixtlan/user_management_controller/user_management_controller_generator.rb +0 -23
  32. data/lib/generators/ixtlan/user_management_models/USAGE +0 -8
  33. data/lib/generators/ixtlan/user_management_models/user_management_models_generator.rb +0 -19
  34. data/lib/generators/ixtlan/user_management_scaffold/user_management_scaffold_generator.rb +0 -13
  35. data/lib/ixtlan/guard/controllers/maintenance_controller.rb +0 -45
  36. data/lib/ixtlan/guard/controllers/permissions_controller.rb +0 -41
  37. data/lib/ixtlan/guard/models/maintenance.rb +0 -55
  38. data/lib/ixtlan/guard/models/user_update_manager.rb +0 -95
  39. data/lib/ixtlan/guard/spec/user_management_models_spec.rb +0 -193
@@ -1,13 +0,0 @@
1
- class Create<%= group_user_name.camelize %> < ActiveRecord::Migration
2
- def self.up
3
- create_table :<%= group_user_name %>, :id => false, :force => true do |t|
4
- <% [group_name, user_name].sort.each do |name| -%>
5
- t.integer :<%= name %>_id
6
- <% end -%>
7
- end
8
- end
9
-
10
- def self.down
11
- drop_table :<%= group_user_name %>
12
- end
13
- end
@@ -1,124 +0,0 @@
1
- require 'ixtlan/guard/models/user_update_manager'
2
- class <%= user_class_name %> < <%= parent_class_name.classify %>
3
- <% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
4
- belongs_to :<%= attribute.name %>
5
- <% end -%>
6
-
7
- has_and_belongs_to_many :<%= plural_group_name %>
8
-
9
- attr_accessor :id
10
-
11
- def self.update_manager
12
- @manager ||= Ixtlan::Guard::Models::UserUpdateManager.new( :group_model => <%= group_class_name %>, :user_id => :<%= user_name %>_id, :group_id => :<%= group_name %>_id, :plural_group_name => :<%= plural_group_name %> )
13
- end
14
-
15
- alias :create! :create
16
- def self.create(params = {})
17
- u = self.new
18
- u.current_user = params.delete("current_user") || params.delete(:current_user)
19
- u.update_attributes(params)
20
- u
21
- end
22
-
23
- def update_attributes(params)
24
- result = []
25
- <% flavors.each do |flavor| %>
26
- # update <%= flavor.pluralize %>
27
- if result.all?{|a| a}
28
- result << self.class.update_manager.update(self, params, :flavor_id => :<%= flavor %>_id, :flavors_for_group => :<%= flavor.pluralize %>_for_<%= group_name %>, :association_model => <%= association_class_name(flavor.pluralize) %>)
29
- end
30
- <% end -%>
31
-
32
- # update <%= plural_group_name %>
33
- self.class.update_manager.update_groups(self, params) if result.all?{|a| a}
34
- end
35
-
36
- unless respond_to? :current_user
37
- # do not use attr_accessor to allow them to be used
38
- # for mass_assignmnet_protection
39
- def current_user=(u)
40
- @current_user = u
41
- end
42
-
43
- def current_user
44
- @current_user
45
- end
46
-
47
- after_save do
48
- @current_user = nil
49
- end
50
- end
51
-
52
- def root?
53
- <%= plural_group_name %>.detect {|g| g.root? } != nil
54
- end
55
-
56
- <% flavors.each do |flavor|
57
- plural_flavor = flavor.pluralize %>
58
- def managed_<%= plural_flavor %>_for_<%= group_name %>(gid)
59
- <%= group_name %> = <%= group_class_name %>.get(gid)
60
- if <%= group_name %>.root?
61
- []
62
- else
63
- existing = <%= plural_flavor %>_for_<%= group_name %>(<%= group_name %>)
64
- managed = <%= plural_flavor %>_for_<%= group_name %>(<%= group_class_name %>.admin_group)
65
- (managed - (managed - existing))
66
- end
67
- end
68
-
69
- def <%= plural_flavor %>_for_<%= group_name %>(group_or_id)
70
- if root?
71
- <%= flavor.camelize %>.all
72
- else
73
- <%= group_name %> = <%= group_class_name %>.get(group_or_id)
74
- <%= association_class_name(plural_flavor) %>.all(:conditions => ["<%= user_name %>_id=? and <%= group_name %>_id=?", id, <%= group_name %>.id]).collect { |dgu| dgu.<%= flavor %> }
75
- end
76
- end
77
-
78
- def <%= flavor %>_<%= group_name %>?(<%= flavor %>, <%= group_name %>)
79
- <%= association_class_name(plural_flavor) %>.count(:conditions => ["<%= user_name %>_id=? and <%= group_name %>_id=? and <%= flavor %>_id=?", id, <%= group_name %>.id, <%= flavor %>.id]) == 1
80
- end
81
-
82
- <% end -%>
83
-
84
- def all_<%= plural_group_name %>
85
- if root?
86
- <%= group_class_name %>.all
87
- else
88
- <%= plural_group_name %>
89
- end
90
- end
91
-
92
- def to_xml
93
- to_hash.to_xml(:root => "<%= file_name %>")
94
- end
95
-
96
- def to_json
97
- { :<%= file_name %> => to_hash }.to_json
98
- end
99
-
100
- def to_hash
101
- map = attributes.dup
102
- g = (map[:<%= plural_group_name %>] = [])
103
- <%= plural_group_name %>.each do |<%= group_name %>|
104
- gg = <%= group_name %>.attributes.dup
105
- gg.delete("<%= group_name %>_id")
106
- gg.delete("<%= file_name %>_id")
107
- <% flavors.each do |flavor|
108
- plural_flavor = flavor.pluralize %>
109
- <%= plural_flavor %> = <%= plural_flavor %>_for_<%= group_name %>(<%= group_name %>)
110
- if <%= plural_flavor %>.size > 0
111
- d = (gg[:<%= plural_flavor %>] = [])
112
- <%= plural_flavor %>.each do |<%= flavor %>|
113
- dd = <%= flavor %>.attributes.dup
114
- dd.delete("created_at")
115
- dd.delete("updated_at")
116
- d << dd
117
- end
118
- end
119
- <% end -%>
120
- g << gg
121
- end
122
- map
123
- end
124
- end
@@ -1,202 +0,0 @@
1
- require 'rails/generators/active_record/model/model_generator'
2
- module ActiveRecord
3
- class UserManagementModelsGenerator < ::ActiveRecord::Generators::ModelGenerator
4
-
5
- source_paths << ActiveRecord::Generators::ModelGenerator.source_root
6
-
7
- source_root File.expand_path('../templates', __FILE__)
8
-
9
- arguments.clear # clear name argument from NamedBase
10
-
11
- argument :params, :type => :array, :required => true
12
-
13
- remove_hook_for :test_framework
14
-
15
- def name
16
- @name ||= user_params[0]
17
- end
18
-
19
- def attributes
20
- @attributes ||= user_params[1,10000]
21
- end
22
-
23
- def attributes=(a = nil)
24
- @attributes = a
25
- end
26
-
27
- protected
28
- def user_params
29
- retrieve_params(@params)
30
- end
31
-
32
- def group_params
33
- result = retrieve_params(@params[user_params.size, 10000])
34
- case result.size
35
- when 0
36
- ["group", "name:string"]
37
- when 1
38
- result << "name:string"
39
- else
40
- result
41
- end
42
- end
43
-
44
- def flavor_params(index, count = 0)
45
- count = user_params.size + retrieve_params(@params[user_params.size, 10000]).size if count == 0
46
- if(index > 0)
47
- count += retrieve_params(@params[count, 10000]).size
48
- if count != @params.size
49
- flavor_params(index - 1, count)
50
- end
51
- else
52
- retrieve_params(@params[count, 10000])
53
- end
54
- end
55
-
56
- def retrieve_params(params)
57
- done = nil
58
- params.select do |para|
59
- if done.nil?
60
- done = false
61
- true
62
- elsif done
63
- false
64
- else
65
- done = (para =~ /[^:]+\:[^:]+/).nil?
66
- !done
67
- end
68
- end
69
- end
70
-
71
- public
72
-
73
- check_class_collision
74
-
75
- class_option :migration, :type => :boolean
76
- class_option :timestamps, :type => :boolean
77
- class_option :parent, :type => :string, :desc => "The parent class for the generated model"
78
-
79
- def user_name
80
- @user_name ||= user_params[0].singularize
81
- end
82
-
83
- def group_name
84
- @group_name ||= group_params[0].singularize
85
- end
86
-
87
- def group_field_name
88
- @group_field_name ||= (group_params[1] || "name:").sub(/\:.*/, '')
89
- end
90
-
91
- def plural_user_name
92
- @plural_user_name ||= user_name.pluralize
93
- end
94
-
95
- def plural_group_name
96
- @plural_group_name ||= group_name.pluralize
97
- end
98
-
99
- def user_class_name
100
- @user_class_name ||= user_name.camelize
101
- end
102
-
103
- def group_class_name
104
- @group_class_name ||= group_name.camelize
105
- end
106
-
107
- def association_name
108
- @association_name ||= [plural_name, plural_group_name, plural_user_name].sort.join("_")
109
- end
110
-
111
- def group_user_name
112
- @group_user_name ||= [plural_group_name, plural_user_name].sort.join("_")
113
- end
114
-
115
- def flavors
116
- @flavors ||= params_array[2,10000].collect {|c| c[0].to_s }
117
- end
118
-
119
- def create_migration_file
120
- return unless options[:migration] && options[:parent].nil?
121
-
122
- index = 0
123
- params_array.each do |params|
124
- setup(params)
125
- migration_template "migration.rb", "db/migrate/create_#{table_name}.rb"
126
- index += 1
127
- if(index > 2)
128
- migration_template "flavor_migration.rb", "db/migrate/create_#{association_name}.rb"
129
-
130
- end
131
- end
132
- migration_template "group_user_migration.rb", "db/migrate/create_#{group_user_name}.rb"
133
- end
134
-
135
- def create_model_file
136
- setup(user_params)
137
- template 'user_model.rb', File.join('app/models', class_path, "#{file_name}.rb")
138
-
139
- setup(group_params)
140
- template 'group_model.rb', File.join('app/models', class_path, "#{file_name}.rb")
141
-
142
- if params_array.size > 2
143
- params_array[2,10000].each do |params|
144
- setup(params)
145
- template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
146
- template 'flavor_model.rb', File.join('app/models', class_path, "#{association_name.singularize}.rb")
147
- end
148
- end
149
- end
150
-
151
- def create_module_file
152
- params_array.each do |params|
153
- assign_names!(params[0])
154
- unless class_path.empty?
155
- template 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb") if behavior == :invoke
156
- end
157
- end
158
- end
159
-
160
- protected
161
-
162
- def association_class_name(plural_flavor)
163
- @association_class_name ||= {}
164
- @association_class_name[plural_flavor] ||= [plural_flavor, plural_group_name, plural_user_name].sort.join("_").singularize.camelize
165
- end
166
-
167
- def setup(params)
168
- @name = params[0]
169
- assign_names!(params[0])
170
- self.attributes = params[1, 10000]
171
- parse_attributes!
172
- @file_path = nil
173
- @class_name = nil
174
- @human_name = nil
175
- @plural_name = nil
176
- @i18n_scope = nil
177
- @table_name = nil
178
- @singular_table_name = nil
179
- @plural_table_name = nil
180
- @plural_file_name = nil
181
- @route_url = nil
182
- @association_name = nil
183
- end
184
-
185
- def params_array
186
- @array ||= begin
187
- result = [user_params, group_params]
188
- index = 0
189
- while( flavor = flavor_params(index) ) do
190
- result << flavor if flavor.size > 0
191
- index += 1
192
- end
193
- result
194
- end
195
- end
196
-
197
- def parent_class_name
198
- options[:parent] || "ActiveRecord::Base"
199
- end
200
-
201
- end
202
- end
@@ -1,10 +0,0 @@
1
- require 'rails/generators/erb'
2
- require 'rails/generators/resource_helpers'
3
-
4
- module Erb
5
- class UserManagementControllerGenerator < ::Erb::Generators::ScaffoldGenerator
6
-
7
- source_root File.expand_path('../templates', __FILE__)
8
-
9
- end
10
- end
@@ -1,8 +0,0 @@
1
- Description:
2
- Explain the generator
3
-
4
- Example:
5
- rails generate guard Thing
6
-
7
- This will create:
8
- what/will/it/create
@@ -1,40 +0,0 @@
1
- require 'rails/generators/resource_helpers'
2
- require 'generators/ixtlan/scaffold/scaffold_generator.rb'
3
- ActiveSupport::Inflector.inflections do |inflect|
4
- inflect.irregular 'maintenance', 'maintenance'
5
- end
6
- module Ixtlan
7
- class MaintenanceScaffoldGenerator < ScaffoldGenerator
8
-
9
- source_root File.expand_path('../../templates', __FILE__)
10
-
11
- arguments.clear # clear name argument from NamedBase
12
-
13
- def name # set alias so NamedBase uses the model as its name
14
- "maintenance"
15
- end
16
-
17
- def create_controller_files
18
- template 'simple_controller.rb', File.join('app', 'controllers', class_path, "#{plural_file_name}_controller.rb")
19
- end
20
-
21
- def add_routes
22
- actions.reverse.each do |action|
23
- if action == 'index'
24
- route %{get "#{file_name}/#{action}"}
25
- else
26
- route %{put "#{file_name}/#{action}"}
27
- end
28
- end
29
- end
30
-
31
- def aliases
32
- {}
33
- end
34
-
35
- def actions
36
- ['index', 'block', 'resume']
37
- end
38
-
39
- end
40
- end
@@ -1,8 +0,0 @@
1
- Description:
2
- Explain the generator
3
-
4
- Example:
5
- rails generate guard Thing
6
-
7
- This will create:
8
- what/will/it/create
@@ -1,33 +0,0 @@
1
- require 'rails/generators/resource_helpers'
2
- require 'generators/ixtlan/scaffold/scaffold_generator.rb'
3
- module Ixtlan
4
- class PermissionsScaffoldGenerator < ScaffoldGenerator
5
-
6
- source_root File.expand_path('../../templates', __FILE__)
7
-
8
- arguments.clear # clear name argument from NamedBase
9
-
10
- def name # set alias so NamedBase uses the model as its name
11
- "permission"
12
- end
13
-
14
- def create_controller_files
15
- template 'simple_controller.rb', File.join('app', 'controllers', class_path, "#{plural_file_name}_controller.rb")
16
- end
17
-
18
- def add_routes
19
- actions.keys.reverse.each do |action|
20
- route %{get "#{file_name}/#{action}"}
21
- end
22
- end
23
-
24
- def aliases
25
- {}
26
- end
27
-
28
- def actions
29
- {'index' => [:*], 'show' => []}
30
- end
31
-
32
- end
33
- end