effective_resources 0.9.5 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/concerns/effective/crud_controller.rb +23 -546
  3. data/app/controllers/concerns/effective/crud_controller/actions.rb +289 -0
  4. data/app/controllers/concerns/effective/crud_controller/dsl.rb +81 -0
  5. data/app/controllers/concerns/effective/crud_controller/paths.rb +87 -0
  6. data/app/controllers/concerns/effective/crud_controller/permitted_params.rb +66 -0
  7. data/app/controllers/concerns/effective/crud_controller/save.rb +77 -0
  8. data/app/controllers/concerns/effective/crud_controller/submits.rb +122 -0
  9. data/app/helpers/effective_resources_helper.rb +51 -52
  10. data/app/helpers/effective_resources_private_helper.rb +69 -0
  11. data/app/models/concerns/effective_resource.rb +24 -0
  12. data/app/models/effective/model_reader.rb +29 -0
  13. data/app/models/effective/resource.rb +6 -2
  14. data/app/models/effective/resources/actions.rb +10 -10
  15. data/app/models/effective/resources/associations.rb +5 -0
  16. data/app/models/effective/resources/attributes.rb +40 -27
  17. data/app/models/effective/resources/controller.rb +81 -0
  18. data/app/models/effective/resources/forms.rb +0 -51
  19. data/app/models/effective/resources/init.rb +19 -17
  20. data/app/models/effective/resources/klass.rb +6 -8
  21. data/app/models/effective/resources/model.rb +23 -0
  22. data/app/models/effective/resources/naming.rb +7 -3
  23. data/app/models/effective/resources/paths.rb +4 -63
  24. data/app/models/effective/resources/relation.rb +4 -1
  25. data/app/models/effective/resources/sql.rb +1 -1
  26. data/app/views/application/create.js.erb +1 -1
  27. data/app/views/application/edit.html.haml +2 -2
  28. data/app/views/application/index.html.haml +1 -1
  29. data/app/views/application/member_action.js.erb +1 -1
  30. data/app/views/application/new.html.haml +3 -1
  31. data/app/views/application/show.html.haml +1 -1
  32. data/app/views/application/update.js.erb +1 -1
  33. data/app/views/effective/resource/_actions.html.haml +3 -32
  34. data/app/views/effective/resource/_actions_dropleft.html.haml +4 -26
  35. data/app/views/effective/resource/_actions_glyphicons.html.haml +4 -10
  36. data/lib/effective_resources/engine.rb +1 -0
  37. data/lib/effective_resources/version.rb +1 -1
  38. metadata +14 -3
@@ -7,7 +7,7 @@ module Effective
7
7
  end
8
8
 
9
9
  def datatable_klass
10
- @datatable_klass ||= if defined?(EffectiveDatatables)
10
+ if defined?(EffectiveDatatables)
11
11
  "#{namespaced_class_name.pluralize}Datatable".safe_constantize ||
12
12
  "#{class_name.pluralize.camelize}Datatable".safe_constantize ||
13
13
  "#{name.pluralize.camelize}Datatable".safe_constantize ||
@@ -18,13 +18,11 @@ module Effective
18
18
  end
19
19
 
20
20
  def controller_klass
21
- @controller_klass ||= (
22
- "#{namespaced_class_name.pluralize}Controller".safe_constantize ||
23
- "#{class_name.pluralize.classify}Controller".safe_constantize ||
24
- "#{name.pluralize.classify}Controller".safe_constantize ||
25
- "#{initialized_name.to_s.classify.pluralize}Controller".safe_constantize ||
26
- "#{initialized_name.to_s.classify}Controller".safe_constantize
27
- )
21
+ "#{namespaced_class_name.pluralize}Controller".safe_constantize ||
22
+ "#{class_name.pluralize.classify}Controller".safe_constantize ||
23
+ "#{name.pluralize.classify}Controller".safe_constantize ||
24
+ "#{initialized_name.to_s.classify.pluralize}Controller".safe_constantize ||
25
+ "#{initialized_name.to_s.classify}Controller".safe_constantize
28
26
  end
29
27
 
30
28
  def active_record?
@@ -0,0 +1,23 @@
1
+ module Effective
2
+ module Resources
3
+ module Model
4
+ attr_accessor :model # As defined by effective_resource do block in a model file
5
+
6
+ def _initialize_model(&block)
7
+ @model = ModelReader.new(&block)
8
+
9
+ # If effective_developer is in live mode, this will cause it to refresh the class
10
+ ActiveSupport.run_load_hooks(:effective_resource, klass)
11
+ end
12
+
13
+ def model
14
+ @model || (klass.effective_resource.model if klass.respond_to?(:effective_resource) && klass.effective_resource)
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+
21
+
22
+
23
+
@@ -4,7 +4,7 @@ module Effective
4
4
  SPLIT = /\/|::/ # / or ::
5
5
 
6
6
  def name # 'post'
7
- @name ||= (klass.try(:name).to_s.split(SPLIT).last || '').singularize.underscore
7
+ @name ||= ((klass.present? ? klass.name : initialized_name).to_s.split(SPLIT).last || '').singularize.underscore
8
8
  end
9
9
 
10
10
  def plural_name # 'posts'
@@ -16,7 +16,7 @@ module Effective
16
16
  end
17
17
 
18
18
  def class_name # 'Effective::Post'
19
- @model_klass.try(:name).to_s
19
+ @model_klass ? @model_klass.name : name.classify
20
20
  end
21
21
 
22
22
  def class_path # 'effective'
@@ -32,13 +32,17 @@ module Effective
32
32
  end
33
33
 
34
34
  def namespaces # ['admin', 'things']
35
- @namespaces
35
+ @namespaces || []
36
36
  end
37
37
 
38
38
  def human_name
39
39
  class_name.gsub('::', ' ').underscore.gsub('_', ' ')
40
40
  end
41
41
 
42
+ def human_plural_name
43
+ class_name.pluralize.gsub('::', ' ').underscore.gsub('_', ' ')
44
+ end
45
+
42
46
  end
43
47
  end
44
48
  end
@@ -2,69 +2,6 @@ module Effective
2
2
  module Resources
3
3
  module Paths
4
4
 
5
- #
6
- # TODO: Delete these. Once effective_developer is updated
7
- #
8
- # # Controller REST helper paths
9
- # def index_path(check: false)
10
- # path = [namespace, plural_name, 'path'].compact * '_'
11
- # path if (!check || path_exists?(path))
12
- # end
13
-
14
- # def new_path(check: false)
15
- # path = ['new', namespace, name, 'path'].compact * '_'
16
- # path if (!check || path_exists?(path))
17
- # end
18
-
19
- # def show_path(check: false)
20
- # path = [namespace, name, 'path'].compact * '_'
21
- # path if (!check || path_exists?(path, 1))
22
- # end
23
-
24
- # def destroy_path(check: false)
25
- # path = [namespace, name, 'path'].compact * '_'
26
- # path if (!check || path_exists?(path, 1, :delete))
27
- # end
28
-
29
- # def edit_path(check: false)
30
- # path = ['edit', namespace, name, 'path'].compact * '_'
31
- # path if (!check || path_exists?(path, 1))
32
- # end
33
-
34
- # def action_path(action, check: false)
35
- # path = [action, namespace, name, 'path'].compact * '_'
36
- # path if (!check || path_exists?(path, 1, :any))
37
- # end
38
-
39
- # def action_post_path(action, check: false)
40
- # path = [action, namespace, name, 'path'].compact * '_'
41
- # path if (!check || path_exists?(path, 1, :post) || path_exists?(path, 1, :put) || path_exists?(path, 1, :patch))
42
- # end
43
-
44
- # def path_exists?(path, param = nil, verb = :get)
45
- # routes = Rails.application.routes
46
-
47
- # return false unless routes.url_helpers.respond_to?(path)
48
- # (routes.recognize_path(routes.url_helpers.send(path, param), method: verb).present? rescue false)
49
- # end
50
-
51
- # # _helper methods also put in the (@thing)
52
- # alias_method :index_path_helper, :index_path
53
- # alias_method :new_path_helper, :new_path
54
-
55
- # def show_path_helper(at: true)
56
- # show_path + '(' + (at ? '@' : '') + name + ')'
57
- # end
58
-
59
- # def edit_path_helper(at: true)
60
- # edit_path + '(' + (at ? '@' : '') + name + ')'
61
- # end
62
-
63
- # def action_path_helper(action, at: true)
64
- # action_path(action) + '(' + (at ? '@' : '') + name + ')'
65
- # end
66
-
67
- # Default file paths
68
5
  def model_file
69
6
  File.join('app/models', class_path.to_s, "#{name}.rb")
70
7
  end
@@ -81,6 +18,10 @@ module Effective
81
18
  File.join('app/views', namespace.to_s, (namespace.present? ? '' : class_path), plural_name, "#{'_' if partial}#{action}.html.haml")
82
19
  end
83
20
 
21
+ def flat_view_file(action = :index, partial: false)
22
+ File.join('app/views', plural_name, "#{'_' if partial}#{action}.html.haml")
23
+ end
24
+
84
25
  end
85
26
  end
86
27
  end
@@ -1,7 +1,10 @@
1
1
  module Effective
2
2
  module Resources
3
3
  module Relation
4
- attr_reader :relation
4
+
5
+ def relation
6
+ @relation ||= klass.where(nil)
7
+ end
5
8
 
6
9
  # When Effective::Resource is initialized with an ActiveRecord relation, the following
7
10
  # methods will be available to operate on that relation, and be chainable and such
@@ -32,7 +32,7 @@ module Effective
32
32
  end
33
33
 
34
34
  def sql_direction(name)
35
- name.to_s.downcase == 'desc' ? 'DESC' : 'ASC'
35
+ name.to_s.downcase == 'desc' ? 'DESC'.freeze : 'ASC'.freeze
36
36
  end
37
37
 
38
38
  # This is for EffectiveDatatables (col as:)
@@ -1,5 +1,5 @@
1
1
  <% resource = (@_effective_resource || Effective::Resource.new(controller_path)) %>
2
2
  <% @resource = instance_variable_get('@' + resource.name) if resource.name %>
3
3
 
4
- EffectiveForm.remote_form_payload = "<%= j render_resource_form(resource) %>";
4
+ EffectiveForm.remote_form_payload = "<%= j render_resource_form(@resource) %>";
5
5
  EffectiveForm.remote_form_flash = <%= raw flash.to_json %>;
@@ -6,6 +6,6 @@
6
6
  .col-8
7
7
  %h1= @page_title
8
8
  .col-4.text-right
9
- = render_resource_actions(resource, @resource, edit: false)
9
+ = render_resource_buttons(@resource, edit: false)
10
10
 
11
- = render_resource_form(resource)
11
+ = render_resource_form(@resource)
@@ -4,7 +4,7 @@
4
4
  .col-8
5
5
  %h1= @page_title
6
6
  .col-4.text-right
7
- = render_resource_actions(resource)
7
+ = render_resource_actions(resource.klass, index: false)
8
8
 
9
9
  - if @datatable
10
10
  = render_datatable(@datatable)
@@ -1,5 +1,5 @@
1
1
  <% resource = (@_effective_resource || Effective::Resource.new(controller_path)) %>
2
2
  <% @resource = instance_variable_get('@' + resource.name) if resource.name %>
3
3
 
4
- EffectiveForm.remote_form_payload = "<%= j render_resource_form(resource, action: action) %>";
4
+ EffectiveForm.remote_form_payload = "<%= j render_resource_form(@resource, action: action) %>";
5
5
  EffectiveForm.remote_form_flash = <%= raw flash.to_json %>;
@@ -5,5 +5,7 @@
5
5
  .row
6
6
  .col-8
7
7
  %h1= @page_title
8
+ .col-4.text-right
9
+ = render_resource_buttons(@resource)
8
10
 
9
- = render_resource_form(resource)
11
+ = render_resource_form(@resource)
@@ -6,7 +6,7 @@
6
6
  .col-8
7
7
  %h1= @page_title
8
8
  .col-4.text-right
9
- = render_resource_actions(resource, @resource, show: false)
9
+ = render_resource_buttons(@resource, show: false)
10
10
 
11
11
  = render @resource
12
12
 
@@ -1,5 +1,5 @@
1
1
  <% resource = (@_effective_resource || Effective::Resource.new(controller_path)) %>
2
2
  <% @resource = instance_variable_get('@' + resource.name) if resource.name %>
3
3
 
4
- EffectiveForm.remote_form_payload = "<%= j render_resource_form(resource) %>";
4
+ EffectiveForm.remote_form_payload = "<%= j render_resource_form(@resource) %>";
5
5
  EffectiveForm.remote_form_flash = <%= raw flash.to_json %>;
@@ -1,34 +1,5 @@
1
- - if actions.index(:new)
2
- - if EffectiveResources.authorized?(controller, :new, effective_resource.klass)
3
- - name = effective_resource.human_name.titleize
4
- = link_to "New #{name}", effective_resource.action_path(:new), title: "New #{name}", class: 'btn btn-primary'
5
-
6
- - if actions.index(:edit)
7
- - if EffectiveResources.authorized?(controller, :edit, resource)
8
- = link_to 'Edit', effective_resource.action_path(:edit, resource), title: "Edit #{resource}", class: 'btn btn-secondary'
9
-
10
- - if actions.index(:show)
11
- - if EffectiveResources.authorized?(controller, :show, resource)
12
- = link_to 'View', effective_resource.action_path(:show, resource), title: resource.to_s, class: 'btn btn-secondary'
13
-
14
- - (actions - effective_resource.crud_actions).each do |action|
15
- - if effective_resource.member_post_actions.include?(action)
16
- - if EffectiveResources.authorized?(controller, action, resource)
17
- = link_to action.to_s.titleize, effective_resource.action_path(action, resource), class: 'btn btn-secondary',
18
- title: "#{action} #{resource}", data: { method: :post, confirm: "#{action.to_s.titleize} #{resource}?"}
19
-
20
- - elsif effective_resource.member_get_actions.include?(action)
21
- - if EffectiveResources.authorized?(controller, action, resource)
22
- = link_to action.to_s.titleize, effective_resource.action_path(action, resource), class: 'btn btn-secondary',
23
- title: "#{action.to_s.titleize} #{resource}"
24
- - else
25
- - if EffectiveResources.authorized?(controller, action, effective_resource.klass)
26
- = link_to action.to_s.titleize, effective_resource.action_path(action), class: 'btn btn-secondary',
27
- title: action.to_s.titleize
1
+ - permitted_resource_actions(resource, actions, effective_resource).each do |label, opts|
2
+ = link_to(label, (effective_resource.action_path(opts[:action], resource) || '#'), opts.except(:action))
28
3
 
4
+ = instance_exec(resource, &format_block) if local_assigns[:format_block]
29
5
  = yield if block_given?
30
-
31
- - if actions.index(:destroy)
32
- - if EffectiveResources.authorized?(controller, :destroy, resource)
33
- = link_to 'Delete', effective_resource.action_path(:destroy, resource), class: 'btn btn-danger',
34
- title: "Delete #{resource}", data: { method: :delete, confirm: "Delete #{resource}?" }
@@ -1,28 +1,6 @@
1
1
  = dropdown(variation: :dropleft) do
2
- - if actions.index(:edit)
3
- - if EffectiveResources.authorized?(controller, :edit, resource)
4
- = dropdown_link_to 'Edit', effective_resource.action_path(:edit, resource), title: "Edit #{resource}"
2
+ - permitted_resource_actions(resource, actions, effective_resource).each do |label, opts|
3
+ = dropdown_link_to(label, (effective_resource.action_path(opts[:action], resource) || '#'), opts.except(:action, :class))
5
4
 
6
- - if actions.index(:show)
7
- - if EffectiveResources.authorized?(controller, :show, resource)
8
- = dropdown_link_to 'View', effective_resource.action_path(:show, resource), title: resource.to_s
9
-
10
- - (actions - effective_resource.crud_actions).each do |action|
11
- - if effective_resource.member_post_actions.include?(action)
12
- - if EffectiveResources.authorized?(controller, action, resource)
13
- = dropdown_link_to action.to_s.titleize, effective_resource.action_path(action, resource),
14
- title: "#{action} #{resource}", data: { method: :post, confirm: "#{action.to_s.titleize} #{resource}?"}
15
-
16
- - elsif effective_resource.member_get_actions.include?(action)
17
- - if EffectiveResources.authorized?(controller, action, resource)
18
- = dropdown_link_to action.to_s.titleize, effective_resource.action_path(action, resource), title: "#{action.to_s.titleize} #{resource}"
19
- - else
20
- - if EffectiveResources.authorized?(controller, action, effective_resource.klass)
21
- = dropdown_link_to action.to_s.titleize, effective_resource.action_path(action), title: action.to_s.titleize
22
-
23
- = yield if block_given?
24
-
25
- - if actions.index(:destroy)
26
- - if EffectiveResources.authorized?(controller, :destroy, resource)
27
- = dropdown_link_to 'Delete', effective_resource.action_path(:destroy, resource),
28
- title: "Delete #{resource}", data: { method: :delete, confirm: "Delete #{resource}?" }
5
+ = instance_exec(resource, &format_block) if local_assigns[:format_block]
6
+ = yield(resource) if block_given?
@@ -1,11 +1,5 @@
1
- - if actions.index(:edit)
2
- - if EffectiveResources.authorized?(controller, :edit, resource)
3
- = edit_icon_to effective_resource.action_path(:edit, resource)
1
+ - permitted_resource_actions(resource, actions, effective_resource).each do |label, opts|
2
+ = glyphicon_to(opts[:action], (effective_resource.action_path(opts[:action], resource) || '#'), opts.except(:action, :class))
4
3
 
5
- - if actions.index(:show)
6
- - if EffectiveResources.authorized?(controller, :show, resource)
7
- = show_icon_to effective_resource.action_path(:show, resource)
8
-
9
- - if actions.index(:destroy)
10
- - if EffectiveResources.authorized?(controller, :destroy, resource)
11
- = destroy_icon_to effective_resource.action_path(:destroy, resource), data: { method: :delete, confirm: "Really delete #{resource}?" }
4
+ = instance_exec(resource, &format_block) if local_assigns[:format_block]
5
+ = yield(resource) if block_given?
@@ -21,6 +21,7 @@ module EffectiveResources
21
21
  ActiveSupport.on_load :active_record do
22
22
  ActiveRecord::Base.extend(ActsAsTokened::ActiveRecord)
23
23
  ActiveRecord::Base.extend(ActsAsSlugged::ActiveRecord)
24
+ ActiveRecord::Base.extend(EffectiveResource::ActiveRecord)
24
25
  end
25
26
  end
26
27
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '0.9.5'.freeze
2
+ VERSION = '0.10.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-13 00:00:00.000000000 Z
11
+ date: 2018-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -36,21 +36,32 @@ files:
36
36
  - app/assets/javascripts/effective_resources.js
37
37
  - app/assets/javascripts/effective_resources/effective_ujs.js
38
38
  - app/controllers/concerns/effective/crud_controller.rb
39
+ - app/controllers/concerns/effective/crud_controller/actions.rb
40
+ - app/controllers/concerns/effective/crud_controller/dsl.rb
41
+ - app/controllers/concerns/effective/crud_controller/paths.rb
42
+ - app/controllers/concerns/effective/crud_controller/permitted_params.rb
43
+ - app/controllers/concerns/effective/crud_controller/save.rb
44
+ - app/controllers/concerns/effective/crud_controller/submits.rb
39
45
  - app/controllers/concerns/effective/flash_messages.rb
40
46
  - app/helpers/effective_resources_helper.rb
47
+ - app/helpers/effective_resources_private_helper.rb
41
48
  - app/models/concerns/acts_as_slugged.rb
42
49
  - app/models/concerns/acts_as_tokened.rb
50
+ - app/models/concerns/effective_resource.rb
43
51
  - app/models/effective/access_denied.rb
44
52
  - app/models/effective/attribute.rb
45
53
  - app/models/effective/code_reader.rb
54
+ - app/models/effective/model_reader.rb
46
55
  - app/models/effective/resource.rb
47
56
  - app/models/effective/resources/actions.rb
48
57
  - app/models/effective/resources/associations.rb
49
58
  - app/models/effective/resources/attributes.rb
59
+ - app/models/effective/resources/controller.rb
50
60
  - app/models/effective/resources/forms.rb
51
61
  - app/models/effective/resources/init.rb
52
62
  - app/models/effective/resources/instance.rb
53
63
  - app/models/effective/resources/klass.rb
64
+ - app/models/effective/resources/model.rb
54
65
  - app/models/effective/resources/naming.rb
55
66
  - app/models/effective/resources/paths.rb
56
67
  - app/models/effective/resources/relation.rb
@@ -92,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
103
  version: '0'
93
104
  requirements: []
94
105
  rubyforge_project:
95
- rubygems_version: 2.4.5.1
106
+ rubygems_version: 2.5.2.3
96
107
  signing_key:
97
108
  specification_version: 4
98
109
  summary: Make any controller an effective resource controller.