active_model_serializers_cancancan 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ce161c1d6f79a45223d67cf65bfc0784d4a0b821
4
+ data.tar.gz: e3f6e01156ac758f0bf84993dcdde3483a519a7b
5
+ SHA512:
6
+ metadata.gz: f7786db0b565437bc845468150c071bc6ffe83af441c57ed6c231c41a281fdc7ad02782fd690edee1267bb88177e793965d8d903d79ecf68f24fc86da30de7bf
7
+ data.tar.gz: 7b351437cb30afabc1cff37e0aa288ddf87957e2b4a241bca1dffd2e8015f015585123e2535b85bba924df153878e1597d858515d76b3ab63f69f7af201f1b26
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ ams-cc
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.2
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 1.9.3
3
+ before_install:
4
+ - gem install bundler --version '>= 1.2.2'
5
+ script: "bundle exec rspec"
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_model_serializers-cancan.gemspec
4
+ gemspec
5
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Gordon L. Hempton
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # ActiveModelSerializers::Cancan
2
+
3
+ Provides a simple integration between [CanCanCan](https://github.com/CanCanCommunity/cancancan) and [Active Model Serializers](https://github.com/rails-api/active_model_serializers).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'active_model_serializers_cancancan'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install active_model_serializers_cancancan
18
+
19
+ ## Usage
20
+
21
+ ### Associations
22
+
23
+ `hasOne` and `hasMany` serializer macros now support an additional property, `authorize`. Associations with this property set to true will be authorized and filtered via CanCan. For example:
24
+
25
+ ```ruby
26
+ class PostSerializer < ActiveModel::Serializer
27
+ attributes :title, :content
28
+
29
+ has_one :author, authorize: true
30
+ has_many :comments, authorize: true
31
+ end
32
+ ```
33
+
34
+ ### Helpers
35
+
36
+ Serializers now also have access to the same helpers as controllers, namely `current_ability`, `can?`, and `cannot?`.
37
+
38
+ ### Ability Serialization
39
+
40
+ Use the `abilities` helper method to add an `abilities` key to the serialized data. For example:
41
+
42
+ ```ruby
43
+ class PostSerializer < ActiveModel::Serializer
44
+ attributes :id
45
+ abilities :show, :update
46
+ end
47
+
48
+ @post.as_json # { id: 1, abilities: { show: true, update: false } }
49
+ ```
50
+
51
+ #### RESTful Alias
52
+
53
+ If `:restful` is passed as an ability it will expand to the 7 default
54
+ RESTful actions: `:index, :show, :new, :create, :edit, :update, :destroy`
55
+
56
+ #### Overriding an Ability
57
+
58
+ Abilities are checked by calling the `can_#{action}?` method. By overriding this method the result for the ability can be customized. For example:
59
+
60
+ ```ruby
61
+ class PostSerializer < ActiveModel::Serializer
62
+ attributes :id
63
+ abilities :show
64
+
65
+ def can_show?
66
+ session[:wizard_started] && can?(:show, object)
67
+ end
68
+ end
69
+ ```
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ # If you want to make this the default task
7
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_model_serializers/cancan/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "active_model_serializers_cancancan"
8
+ gem.version = ActiveModel::Serializers::Cancan::VERSION
9
+ gem.authors = ["GlebTv", "Gordon L. Hempton"]
10
+ gem.email = ["glebtv@gmail.com"]
11
+ gem.summary = %q{CanCanCan integration with Active Model Serializers}
12
+ gem.homepage = "https://github.com/glebtv/active_model_serializers_cancancan"
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_dependency 'active_model_serializers'
20
+ gem.add_dependency 'cancancan'
21
+
22
+ gem.add_development_dependency 'bundler'
23
+ gem.add_development_dependency 'rake'
24
+ gem.add_development_dependency 'rspec'
25
+ gem.add_development_dependency 'pry'
26
+ gem.add_development_dependency 'mongoid', '~> 4.0.0.beta1'
27
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'active_model_serializers/cancan'
@@ -0,0 +1,48 @@
1
+ module ActiveModel
2
+ class Serializer
3
+ module CanCan
4
+ module Abilities
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ class_attribute :cancan_actions
9
+ end
10
+
11
+ def can
12
+ cancan_actions.inject({}) do |hash, action|
13
+ hash[action] = send("can_#{action}?")
14
+ hash
15
+ end
16
+ end
17
+
18
+ module ClassMethods
19
+ def abilities(*actions)
20
+ self.cancan_actions = expand_cancan_actions(actions)
21
+ cancan_actions.each do |action|
22
+ method = "can_#{action}?".to_sym
23
+ unless method_defined?(method)
24
+ define_method method do
25
+ can? action, object
26
+ end
27
+ end
28
+ end
29
+ attributes :can
30
+ end
31
+
32
+ private
33
+ def expand_cancan_actions(actions)
34
+ if actions.include? :restful
35
+ actions.delete :restful
36
+ actions |= [:index, :show, :new, :create, :edit, :update, :destroy]
37
+ end
38
+ actions
39
+ end
40
+ end
41
+
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ ActiveModel::Serializer.send :include, ActiveModel::Serializer::CanCan::Abilities
48
+
@@ -0,0 +1,38 @@
1
+ module ActiveModel
2
+ class Serializer
3
+ module Associations #:nodoc:
4
+
5
+ class Config #:nodoc:
6
+
7
+ def authorize?
8
+ !!options[:authorize]
9
+ end
10
+ end
11
+
12
+ class HasMany #:nodoc:
13
+
14
+ def serialize_with_cancan
15
+ return serialize_without_cancan unless authorize?
16
+ associated_object.select {|item| find_serializable(item).can?(:read, item) }.map do |item|
17
+ find_serializable(item).serializable_hash
18
+ end
19
+ end
20
+ alias_method_chain :serialize, :cancan
21
+
22
+ end
23
+
24
+ class HasOne #:nodoc:
25
+
26
+ def serialize_with_cancan
27
+ object = associated_object
28
+ serializer = find_serializable(object)
29
+ #p !authorize?, serializer, serializer.can?(:read, object)
30
+ return nil unless !authorize? || serializer && serializer.can?(:read, object)
31
+ serialize_without_cancan
32
+ end
33
+ alias_method_chain :serialize, :cancan
34
+
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,21 @@
1
+ module ActiveModel
2
+ class Serializer
3
+ module CanCan
4
+ module Helpers
5
+ def current_ability
6
+ Ability.new(options[:scope])
7
+ end
8
+
9
+ def can?(*args)
10
+ current_ability.can? *args
11
+ end
12
+
13
+ def cannot?
14
+ current_ability.cannot? *args
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ ActiveModel::Serializer.send :include, ActiveModel::Serializer::CanCan::Helpers
@@ -0,0 +1,9 @@
1
+ # A small hack to allow passing options to associations
2
+ class ActiveModel::Serializer
3
+ def include_associations!
4
+ _associations.each_pair do |name, asst|
5
+ include!(name, asst.options) if include?(name)
6
+ end
7
+ end
8
+ end
9
+
@@ -0,0 +1,7 @@
1
+ module ActiveModel
2
+ module Serializers
3
+ module Cancan
4
+ VERSION = "0.0.3"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'cancancan'
2
+ require 'active_model_serializers'
3
+ require 'active_model_serializers/cancan/monkeypatch'
4
+ require 'active_model_serializers/cancan/version'
5
+ require 'active_model_serializers/cancan/helpers'
6
+ require 'active_model_serializers/cancan/associations'
7
+ require 'active_model_serializers/cancan/abilities'
@@ -0,0 +1 @@
1
+ require "active_model_serializers/cancan"
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveModel::Serializer::CanCan::Abilities do
4
+ let(:user) { User.first }
5
+ let(:category) { Category.first }
6
+
7
+ class MockAbility
8
+ include CanCan::Ability
9
+ def initialize(user)
10
+ can :manage, Category
11
+ cannot :read, Category
12
+ end
13
+ end
14
+
15
+ describe '.abilities' do
16
+ let(:serializer) do
17
+ Class.new(ActiveModel::Serializer) do
18
+ attributes :id
19
+ abilities :restful, :foo
20
+ def current_ability
21
+ MockAbility.new(nil)
22
+ end
23
+
24
+ def can_foo?
25
+ true
26
+ end
27
+ end
28
+ end
29
+
30
+ let(:category_serializer) { serializer.new(category, scope: user) }
31
+
32
+ context 'serializable_hash' do
33
+ subject { category_serializer.serializable_hash }
34
+ its(:keys) { should eq [:id, :can] }
35
+ end
36
+
37
+ context 'abilities key' do
38
+ subject { category_serializer.serializable_hash[:can] }
39
+ its([:restful]) { should be_nil }
40
+ its([:update]) { should be_true }
41
+ its([:show]) { should be_false }
42
+ its([:foo]) { should be_true }
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveModel::Serializer::Associations do
4
+
5
+ let(:user) { User.find(1) }
6
+
7
+ let(:category) { Category.first }
8
+
9
+ context 'when authorize is set to false' do
10
+
11
+ before do
12
+ Object.send(:remove_const, :CategorySerializer) if defined?(CategorySerializer)
13
+ Object.send(:remove_const, :ProjectSerializer) if defined?(ProjectSerializer)
14
+ Object.send(:remove_const, :Ability) if defined?(Ability)
15
+
16
+ CategorySerializer = Class.new(ActiveModel::Serializer) do
17
+ attributes :id
18
+ has_many :projects, authorize: false
19
+ has_one :project, authorize: false
20
+ end
21
+
22
+ ProjectSerializer = Class.new(ActiveModel::Serializer) do
23
+ attributes :id
24
+ end
25
+
26
+ Ability = Class.new do
27
+ include CanCan::Ability
28
+ def initialize(user)
29
+ cannot :read, :project
30
+ end
31
+ end
32
+ end
33
+
34
+ it 'should serialize forbidden has_many records' do
35
+ expect(CategorySerializer.new(category, scope: user).serializable_hash[:projects].length).to eq(2)
36
+ end
37
+
38
+ it 'should serialize forbidden has_one records' do
39
+ expect(CategorySerializer.new(category, scope: user).serializable_hash[:project]).to_not be_nil
40
+ end
41
+
42
+ end
43
+
44
+ context 'when authorize set to true' do
45
+
46
+ before do
47
+ Object.send(:remove_const, :CategorySerializer) if defined?(CategorySerializer)
48
+ Object.send(:remove_const, :ProjectSerializer) if defined?(ProjectSerializer)
49
+ Object.send(:remove_const, :Ability) if defined?(Ability)
50
+
51
+ CategorySerializer = Class.new(ActiveModel::Serializer) do
52
+ attributes :id
53
+ has_many :projects, authorize: true
54
+ has_one :project, authorize: true
55
+ end
56
+
57
+ ProjectSerializer = Class.new(ActiveModel::Serializer) do
58
+ attributes :id
59
+ end
60
+
61
+ Ability = Class.new do
62
+ include CanCan::Ability
63
+ def initialize(user)
64
+ can :read, Category
65
+ can :read, Project do |pr|
66
+ pr.user == user
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ it 'should filter unauthorized records', focus: true do
73
+ expect(CategorySerializer.new(category, scope: user).serializable_hash[:projects].length).to eq(1)
74
+ end
75
+
76
+ it 'should nil out unauthorized has_one records' do
77
+ expect(CategorySerializer.new(category, scope: user).serializable_hash[:project]).to be_nil
78
+ end
79
+
80
+ it 'should serialize authorized has_one records' do
81
+ expect(CategorySerializer.new(category, scope: User.find(2)).serializable_hash[:project]).to_not be_nil
82
+ end
83
+
84
+ end
85
+
86
+ end
@@ -0,0 +1,49 @@
1
+ require 'bundler'
2
+
3
+ Bundler.require(:default, :test)
4
+
5
+ require 'mongoid'
6
+ require 'active_model_serializers_cancancan'
7
+
8
+ Mongoid.configure do |config|
9
+ config.connect_to "ams-test"
10
+ end
11
+
12
+ class User
13
+ include Mongoid::Document
14
+ field :name
15
+ has_many :projects
16
+ has_many :categories
17
+ end
18
+
19
+ class Project
20
+ include Mongoid::Document
21
+ belongs_to :user
22
+ belongs_to :category
23
+ has_many :categories
24
+ end
25
+
26
+ class Category
27
+ include Mongoid::Document
28
+ belongs_to :user
29
+ belongs_to :project
30
+ has_many :projects
31
+ end
32
+
33
+ RSpec.configure do |config|
34
+ config.before do
35
+ user1 = User.create(id: 1, name: "User1")
36
+ user2 = User.create(id: 2, name: "User2")
37
+
38
+ c = Category.create(project: Project.create(user: user2))
39
+
40
+ Project.create(user: user1, category: c)
41
+ Project.create(user: user2, category: c)
42
+ end
43
+
44
+ config.after do
45
+ User.delete_all
46
+ Project.delete_all
47
+ Category.delete_all
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_model_serializers_cancancan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - GlebTv
8
+ - Gordon L. Hempton
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-05-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: active_model_serializers
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: cancancan
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: pry
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: mongoid
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: 4.0.0.beta1
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: 4.0.0.beta1
112
+ description:
113
+ email:
114
+ - glebtv@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".ruby-gemset"
121
+ - ".ruby-version"
122
+ - ".travis.yml"
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - active_model_serializers_cancancan.gemspec
128
+ - init.rb
129
+ - lib/active_model_serializers/cancan.rb
130
+ - lib/active_model_serializers/cancan/abilities.rb
131
+ - lib/active_model_serializers/cancan/associations.rb
132
+ - lib/active_model_serializers/cancan/helpers.rb
133
+ - lib/active_model_serializers/cancan/monkeypatch.rb
134
+ - lib/active_model_serializers/cancan/version.rb
135
+ - lib/active_model_serializers_cancancan.rb
136
+ - spec/active_model_serializers/cancan/abilities_spec.rb
137
+ - spec/active_model_serializers/cancan/associations_spec.rb
138
+ - spec/spec_helper.rb
139
+ homepage: https://github.com/glebtv/active_model_serializers_cancancan
140
+ licenses: []
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.2.2
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: CanCanCan integration with Active Model Serializers
162
+ test_files:
163
+ - spec/active_model_serializers/cancan/abilities_spec.rb
164
+ - spec/active_model_serializers/cancan/associations_spec.rb
165
+ - spec/spec_helper.rb