neuron 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/neuron/authorization.rb +64 -8
- data/lib/neuron/controller.rb +2 -5
- data/lib/neuron/railtie.rb +7 -1
- data/lib/neuron/resources.rb +68 -12
- data/lib/neuron/version.rb +1 -1
- data/neuron.gemspec +1 -0
- metadata +17 -7
- data/lib/neuron/integrations/cancan_inherited_resources.rb +0 -83
data/lib/neuron/authorization.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'neuron'
|
2
|
+
require 'cancan'
|
2
3
|
require 'active_support/concern'
|
3
4
|
|
4
5
|
module Neuron
|
@@ -12,20 +13,75 @@ module Neuron
|
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
15
|
-
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
module ClassMethods
|
17
|
+
def authorize_resources(options = {})
|
18
|
+
include Neuron::Authorization::Controller::ControllerExtension
|
19
|
+
|
20
|
+
before_filter :authorize_resource
|
21
|
+
has_scope(:authorize, type: :boolean, default: true) do |controller, scope|
|
22
|
+
scope.accessible_by(controller.current_ability, controller.send(:authorization_action))
|
21
23
|
end
|
22
24
|
end
|
23
|
-
|
24
|
-
|
25
|
+
end
|
26
|
+
|
27
|
+
def access_denied(exception = nil)
|
28
|
+
if user_signed_in?
|
25
29
|
redirect_to '/422.html'
|
30
|
+
else
|
31
|
+
redirect_to new_user_session_path, alert: I18n.t('devise.failure.unauthenticated')
|
26
32
|
end
|
27
33
|
end
|
28
34
|
|
35
|
+
module ControllerExtension
|
36
|
+
protected
|
37
|
+
|
38
|
+
def authorization_action
|
39
|
+
@authorization_action ||= action_name.to_sym
|
40
|
+
end
|
41
|
+
|
42
|
+
def authorization_resource
|
43
|
+
@authorization_resource ||= case authorization_action
|
44
|
+
when :index
|
45
|
+
resource_class
|
46
|
+
when :new, :create
|
47
|
+
build_resource
|
48
|
+
else
|
49
|
+
resource
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def authorize_resource
|
54
|
+
authorize!(authorization_action, authorization_resource)
|
55
|
+
end
|
56
|
+
|
57
|
+
def build_resource
|
58
|
+
get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_build)).tap do |resource|
|
59
|
+
current_ability.attributes_for(authorization_action, resource_class).each do |attribute, value|
|
60
|
+
resource.send("#{attribute}=", value) if attribute.is_a?(Symbol)
|
61
|
+
end
|
62
|
+
resource.attributes = resource_params
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
# Evaluate the parent given. This is used to nest parents in the
|
69
|
+
# association chain.
|
70
|
+
def evaluate_parent(parent_symbol, parent_config, chain = nil) #:nodoc:
|
71
|
+
instantiated_object = instance_variable_get("@#{parent_config[:instance_name]}")
|
72
|
+
return instantiated_object if instantiated_object
|
73
|
+
|
74
|
+
parent = if chain
|
75
|
+
chain.send(parent_config[:collection_name])
|
76
|
+
else
|
77
|
+
parent_config[:parent_class]
|
78
|
+
end.accessible_by(current_ability, :read)
|
79
|
+
|
80
|
+
parent = parent.send(parent_config[:finder], params[parent_config[:param]])
|
81
|
+
|
82
|
+
instance_variable_set("@#{parent_config[:instance_name]}", parent)
|
83
|
+
end
|
84
|
+
end
|
29
85
|
end
|
30
86
|
|
31
87
|
module View
|
data/lib/neuron/controller.rb
CHANGED
@@ -8,11 +8,8 @@ module Neuron
|
|
8
8
|
def self.setup!
|
9
9
|
if defined?(::ActionController)
|
10
10
|
ActionController::Base.send(:include, Neuron::Controller)
|
11
|
-
if defined?(::InheritedResources)
|
12
|
-
|
13
|
-
require 'neuron/integrations/cancan_inherited_resources' if defined?(::CanCan)
|
14
|
-
end
|
15
|
-
ActionController::Base.send(:include, Neuron::Authorization::Controller) if defined?(::CanCan)
|
11
|
+
ActionController::Base.send(:include, Neuron::Resources::Controller) if defined?(::InheritedResources)
|
12
|
+
ActionController::Base.send(:include, Neuron::Authorization::Controller) if defined?(::CanCan)
|
16
13
|
end
|
17
14
|
end
|
18
15
|
|
data/lib/neuron/railtie.rb
CHANGED
@@ -3,7 +3,13 @@ require 'rails'
|
|
3
3
|
require 'rails/engine'
|
4
4
|
|
5
5
|
class Neuron::Railtie < Rails::Engine
|
6
|
-
config.
|
6
|
+
config.neuron = Neuron
|
7
|
+
|
8
|
+
if config.respond_to?(:app_generators)
|
9
|
+
config.app_generators.stylesheets false
|
10
|
+
else
|
11
|
+
config.generators.stylesheets false
|
12
|
+
end
|
7
13
|
|
8
14
|
config.to_prepare do
|
9
15
|
Neuron.setup!
|
data/lib/neuron/resources.rb
CHANGED
@@ -1,38 +1,94 @@
|
|
1
1
|
require 'neuron'
|
2
|
+
require 'inherited_resources'
|
3
|
+
require 'has_scope'
|
2
4
|
|
3
5
|
module Neuron
|
4
6
|
module Resources
|
7
|
+
class << self
|
8
|
+
attr_accessor :default_options
|
9
|
+
end
|
10
|
+
|
11
|
+
self.default_options = {
|
12
|
+
order: true,
|
13
|
+
paginate: defined?(::WillPaginate),
|
14
|
+
authorize: defined?(::CanCan)
|
15
|
+
}
|
16
|
+
|
5
17
|
module Controller
|
6
18
|
extend ActiveSupport::Concern
|
7
19
|
|
8
20
|
module ClassMethods
|
9
21
|
def resources(options = {})
|
22
|
+
options = options.reverse_merge(options)
|
23
|
+
|
24
|
+
unless respond_to?(:resource_options)
|
25
|
+
class_attribute :resource_options
|
26
|
+
end
|
27
|
+
self.resource_options = options
|
28
|
+
prepend_before_filter :set_default_collection_scopes
|
29
|
+
|
10
30
|
inherit_resources
|
11
31
|
append_neuron_view_path_resolver
|
12
|
-
|
32
|
+
|
33
|
+
if options[:order] == true
|
13
34
|
order_scopes
|
14
|
-
elsif options[:
|
15
|
-
order_scopes(options[:
|
35
|
+
elsif options[:order]
|
36
|
+
order_scopes(options[:order])
|
37
|
+
end
|
38
|
+
|
39
|
+
if options[:paginate] == true
|
40
|
+
paginate_scope
|
41
|
+
elsif options[:paginate]
|
42
|
+
paginate_scope(options[:paginate])
|
43
|
+
end
|
44
|
+
|
45
|
+
if options[:authorize]
|
46
|
+
authorize_resources(options[:authorize])
|
16
47
|
end
|
17
48
|
end
|
49
|
+
|
18
50
|
# Adds default ordering scopes for #index action
|
19
51
|
# @param [Hash] options options hash
|
20
52
|
# @option options [Array, Symbol, String] :ascending default ascending scopes
|
21
53
|
# @option options [Array, Symbol, String] :descending default descending scopes
|
22
54
|
def order_scopes(options = {})
|
23
55
|
with_options only: :index, type: :array do |index|
|
24
|
-
index.has_scope(:ascending)
|
25
|
-
index.has_scope(:descending)
|
56
|
+
index.has_scope(:ascending) { |controller, scope, values| values.any? ? scope.asc(*values) : scope }
|
57
|
+
index.has_scope(:descending) { |controller, scope, values| values.any? ? scope.desc(*values) : scope }
|
26
58
|
end
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
59
|
+
end
|
60
|
+
|
61
|
+
def paginate_scope(options = {})
|
62
|
+
define_method(:collection_with_pagination) do
|
63
|
+
get_collection_ivar || begin
|
64
|
+
results = end_of_association_chain
|
65
|
+
if (params[:paginate] != "false") && applicable?(options[:if], true) && applicable?(options[:unless], false)
|
66
|
+
results = results.paginate(page: params[:page], per_page: params[:per_page])
|
67
|
+
headers['X-Pagination-TotalEntries'] = results.total_entries.to_s
|
68
|
+
headers['X-Pagination-TotalPages'] = results.total_pages.to_s
|
69
|
+
headers['X-Pagination-CurrentPage'] = results.current_page.to_s
|
70
|
+
headers['X-Pagination-PerPage'] = results.per_page.to_s
|
71
|
+
end
|
72
|
+
set_collection_ivar(results)
|
32
73
|
end
|
33
|
-
params[:ascending] = Array(params[:ascending])
|
34
|
-
params[:descending] = Array(params[:descending])
|
35
74
|
end
|
75
|
+
alias_method_chain :collection, :pagination
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
protected
|
80
|
+
|
81
|
+
def set_default_collection_scopes
|
82
|
+
if resource_options[:order]
|
83
|
+
unless params[:ascending] || params[:descending]
|
84
|
+
params[:ascending] = resource_options[:order][:ascending] if resource_options[:order][:ascending]
|
85
|
+
params[:descending] = resource_options[:order][:descending] if resource_options[:order][:descending]
|
86
|
+
end
|
87
|
+
params[:ascending] = Array(params[:ascending])
|
88
|
+
params[:descending] = Array(params[:descending])
|
89
|
+
end
|
90
|
+
if resource_options[:paginate]
|
91
|
+
params[:page] ||= 1
|
36
92
|
end
|
37
93
|
end
|
38
94
|
end
|
data/lib/neuron/version.rb
CHANGED
data/neuron.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: neuron
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-06-
|
12
|
+
date: 2011-06-12 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &71321020 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *71321020
|
14
25
|
- !ruby/object:Gem::Dependency
|
15
26
|
name: rake
|
16
|
-
requirement: &
|
27
|
+
requirement: &71320800 !ruby/object:Gem::Requirement
|
17
28
|
none: false
|
18
29
|
requirements:
|
19
30
|
- - ! '>='
|
@@ -21,7 +32,7 @@ dependencies:
|
|
21
32
|
version: '0'
|
22
33
|
type: :development
|
23
34
|
prerelease: false
|
24
|
-
version_requirements: *
|
35
|
+
version_requirements: *71320800
|
25
36
|
description: Code reused in many applications
|
26
37
|
email:
|
27
38
|
- al@semyonov.us
|
@@ -45,7 +56,6 @@ files:
|
|
45
56
|
- lib/neuron/application.rb
|
46
57
|
- lib/neuron/authorization.rb
|
47
58
|
- lib/neuron/controller.rb
|
48
|
-
- lib/neuron/integrations/cancan_inherited_resources.rb
|
49
59
|
- lib/neuron/navigation.rb
|
50
60
|
- lib/neuron/railtie.rb
|
51
61
|
- lib/neuron/resolver.rb
|
@@ -69,7 +79,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
69
79
|
version: '0'
|
70
80
|
segments:
|
71
81
|
- 0
|
72
|
-
hash: -
|
82
|
+
hash: -338995043
|
73
83
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
84
|
none: false
|
75
85
|
requirements:
|
@@ -78,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
88
|
version: '0'
|
79
89
|
segments:
|
80
90
|
- 0
|
81
|
-
hash: -
|
91
|
+
hash: -338995043
|
82
92
|
requirements: []
|
83
93
|
rubyforge_project: neuron
|
84
94
|
rubygems_version: 1.8.5
|
@@ -1,83 +0,0 @@
|
|
1
|
-
require 'inherited_resources'
|
2
|
-
require 'cancan'
|
3
|
-
|
4
|
-
module InheritedResources
|
5
|
-
module Actions
|
6
|
-
# GET /resources/1/edit
|
7
|
-
def edit(options={}, &block)
|
8
|
-
authorize! authorization_action, resource
|
9
|
-
respond_with(*(with_chain(resource) << options), &block)
|
10
|
-
end
|
11
|
-
alias :edit! :edit
|
12
|
-
end
|
13
|
-
|
14
|
-
module BaseHelpers
|
15
|
-
protected
|
16
|
-
|
17
|
-
def resource
|
18
|
-
get_resource_ivar ||
|
19
|
-
set_resource_ivar(end_of_association_chain.accessible_by(current_ability, authorization_action).send(method_for_find, params[:id])).tap do |resource|
|
20
|
-
authorize!(authorization_action, resource)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def build_resource
|
25
|
-
get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_build)).tap do |resource|
|
26
|
-
current_ability.attributes_for(authorization_action, resource_class).each do |attribute, value|
|
27
|
-
resource.send("#{attribute}=", value) if attribute.is_a?(Symbol)
|
28
|
-
end
|
29
|
-
resource.attributes = resource_params
|
30
|
-
authorize!(action_name, resource)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def collection
|
35
|
-
get_collection_ivar || begin
|
36
|
-
authorize!(authorization_action, resource_class)
|
37
|
-
set_collection_ivar(end_of_association_chain.accessible_by(current_ability, authorization_action).paginate(page: params[:page]))
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def create_resource(object)
|
42
|
-
authorize!(authorization_action, object)
|
43
|
-
object.save
|
44
|
-
end
|
45
|
-
|
46
|
-
def update_resource(object, attributes)
|
47
|
-
authorize!(authorization_action, object)
|
48
|
-
object.update_attributes(attributes)
|
49
|
-
end
|
50
|
-
|
51
|
-
def destroy_resource(object)
|
52
|
-
authorize!(authorization_action, object)
|
53
|
-
object.destroy
|
54
|
-
end
|
55
|
-
|
56
|
-
private
|
57
|
-
|
58
|
-
def authorization_action
|
59
|
-
@authorization_action ||= action_name.to_sym
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
module BelongsToHelpers
|
64
|
-
private
|
65
|
-
|
66
|
-
# Evaluate the parent given. This is used to nest parents in the
|
67
|
-
# association chain.
|
68
|
-
def evaluate_parent(parent_symbol, parent_config, chain = nil) #:nodoc:
|
69
|
-
instantiated_object = instance_variable_get("@#{parent_config[:instance_name]}")
|
70
|
-
return instantiated_object if instantiated_object
|
71
|
-
|
72
|
-
parent = if chain
|
73
|
-
chain.send(parent_config[:collection_name])
|
74
|
-
else
|
75
|
-
parent_config[:parent_class]
|
76
|
-
end.accessible_by(current_ability, :read)
|
77
|
-
|
78
|
-
parent = parent.send(parent_config[:finder], params[parent_config[:param]])
|
79
|
-
|
80
|
-
instance_variable_set("@#{parent_config[:instance_name]}", parent)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|