scoped_attributes 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 78381184544a7b5f61b066c19d17878251f8ae47c575386c44ec72a616c16b85
4
- data.tar.gz: f149b82115e1aa5cd9caf2dede0e23a6375aa2eb03a4b22935583cf0afb1606d
3
+ metadata.gz: 58a14ad96009f828c2b5b2c35b5e5ce4d94892bf8b4c2baa91af37a151a4bf41
4
+ data.tar.gz: d6b10e21375ec889036324c6898bcde2071b89e7214dc9b738a257f9d25bea07
5
5
  SHA512:
6
- metadata.gz: '086dfa13d6c36dd9c6ce46de3fe2844ff91de2cbf00258dbbe51418159fbf65590ee66d5f70fd95a480a8c254148cac24c6171ef02eacb5c7da3b12cfc98cbb8'
7
- data.tar.gz: d67ccd206353b78b1ec44ee1a01bca25fea3282da4a1c0a462823eee9e0a82b7c6f0efa89d8a3cb701d7b633060db2a52c0ddeddc3fd92d7d0bdce659925b0f0
6
+ metadata.gz: ca6e6dd8d85d5439bc6a2baeca58891b73e2de07867b4d6ec388f3ba5c0b11c9feed39ba2cc4dc7b13f53b5e574a5392e868c878d9193b4e0732058ab33c288a
7
+ data.tar.gz: b658d27b41b696b2fa7c73c11ddb7b3e22c11b3184cd9297bc6c761e7fd4acded773e458c8b713658b384a80f424e1afa547d46d48c9201884e28081c0c94ae5
@@ -0,0 +1,18 @@
1
+ name: CI
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v1
10
+ - name: Set up Ruby 2.6
11
+ uses: actions/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.6.x
14
+ - name: Build and test with Rake
15
+ run: |
16
+ gem install bundler
17
+ bundle install --jobs 4 --retry 3
18
+ bundle exec rake
data/README.md CHANGED
@@ -1,4 +1,9 @@
1
1
  # ScopedAttributes
2
+ ![](https://github.com/shunhikita/scoped_attributes/workflows/CI/badge.svg?branch=master)
3
+
4
+ scoped_attributes provides a module that allows you to add access restriction settings to attributes.
5
+ By using this, you can easily create a wrapper model that returns only the information required for each different role.
6
+ I think it can also be used to API Scope and view models.
2
7
 
3
8
  ## Installation
4
9
 
@@ -8,29 +13,21 @@ Add this line to your application's Gemfile:
8
13
  gem 'scoped_attributes'
9
14
  ```
10
15
 
11
- And then execute:
12
-
13
- $ bundle
14
-
15
- Or install it yourself as:
16
-
17
- $ gem install scoped_attributes
18
-
19
16
  ## Usage
20
17
 
21
- create scoped class
18
+ You can create a wrapper model by including the ScopedAttributes module.
22
19
 
23
20
  ```rb
24
21
  class ApplicationScopedModel
25
22
  include ScopedAttributes
26
- roles :admin, :crew
23
+ roles :admin, :manager
27
24
 
28
25
  def admin?
29
26
  user.admin?
30
27
  end
31
28
 
32
- def crew?
33
- !admin?
29
+ def manager?
30
+ user.manager?
34
31
  end
35
32
  end
36
33
  ```
@@ -48,13 +45,15 @@ class ScopedCrew < ApplicationScopedModel
48
45
  end
49
46
  ```
50
47
 
51
- ### for ActiveRecord
48
+ ### When using with ActiveRecord object
52
49
 
53
- usage
50
+ Specify the target object in the first argument and the user with the role in the second argument.
51
+ The acquisition of the attribute is controlled by the condition specified in attribute class macro.
54
52
 
55
- current_user is crew and crew is not me
53
+ For an ActiveRecord object, you can use the to_model method to get the model instance with the attribute selected.
56
54
 
57
55
  ```rb
56
+ # current_user is not admin and not me
58
57
  scoped_crew = ScopedCrew.new(Crew.find(1), current_user)
59
58
  scoped_crew.name
60
59
  # => "hoge"
@@ -66,14 +65,15 @@ scoped_crew.attributes
66
65
  # => {:id=>1, :name=>"hoge"}
67
66
 
68
67
  scoped_crew.to_model
69
- => #<Crew:xxx id: 1, name: "hoge">
68
+ # => #<Crew:xxx id: 1, name: "hoge">
70
69
 
71
- Crew.find(1).scoped
72
- => #<Crew:xxx id: 1, name: "hoge">
70
+ Crew.find(1).scoped(current_user)
71
+ # => #<Crew:xxx id: 1, name: "hoge">
73
72
  ```
74
73
 
75
- current_user is crew and crew is me
74
+
76
75
  ```rb
76
+ # current_user is not admin but is me
77
77
  scoped_crew = ScopedCrew.new(Crew.find(1), current_user)
78
78
  scoped_crew.name
79
79
  # => "hoge"
@@ -85,33 +85,34 @@ scoped_crew.attributes
85
85
  # => {:id=>1, :name=>"hoge", :address=>"tokyo"}
86
86
 
87
87
  scoped_crew.to_model
88
- => #<Crew:xxx id: 1, name: "hoge", address: "tokyo">
88
+ # => #<Crew:xxx id: 1, name: "hoge", address: "tokyo">
89
89
 
90
- Crew.find(1).scoped
91
- => #<Crew:xxx id: 1, name: "hoge", address: "tokyo">
90
+ Crew.find(1).scoped(current_user)
91
+ # => #<Crew:xxx id: 1, name: "hoge", address: "tokyo">
92
92
  ```
93
93
 
94
- current_user is admin
95
-
96
94
  ```rb
95
+ # current_user is admin
97
96
  scoped_crew = ScopedCrew.new(Crew.find(1), current_user)
98
97
  scoped_crew.name
99
98
  # => "hoge"
100
99
 
101
100
  scoped_crew.address
102
- # => "tokyo"
101
+ # => "tokyo"
103
102
 
104
103
  scoped_crew.attributes
105
104
  # => {:id=>1, :name=>"hoge", :address=>"tokyo", :evaluation=>"SS"}
106
105
 
107
106
  scoped_crew.to_model
108
- => #<Crew:xxx id: 1, name: "hoge", address: "tokyo", evaluation: "SS">
107
+ # => #<Crew:xxx id: 1, name: "hoge", address: "tokyo", evaluation: "SS">
109
108
 
110
- Crew.find(1).scoped
111
- => #<Crew:xxx id: 1, name: "hoge", address: "tokyo", evaluation: "SS">
109
+ Crew.find(1).scoped(current_user)
110
+ # => #<Crew:xxx id: 1, name: "hoge", address: "tokyo", evaluation: "SS">
112
111
  ```
113
112
 
114
- ## for pure object
113
+ ## When using with PORO
114
+
115
+ ScopedAttributes module can also be used by including it in PORO(Plain Old Ruby Object).
115
116
 
116
117
  ```rb
117
118
  class ScopedCrew < ApplicationScopedModel
@@ -136,13 +137,6 @@ scoped_crew.attributes
136
137
  # => {:name=>"hoge", :address=>"tokyo"}
137
138
  ```
138
139
 
139
-
140
- ## Development
141
-
142
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
143
-
144
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
145
-
146
140
  ## Contributing
147
141
 
148
142
  Bug reports and pull requests are welcome on GitHub at https://github.com/shunhikita/scoped_attributes.
@@ -1,3 +1,5 @@
1
+ require "active_support/concern"
2
+
1
3
  module ScopedAttributes
2
4
  module Scopable
3
5
  extend ActiveSupport::Concern
@@ -1,3 +1,3 @@
1
1
  module ScopedAttributes
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require "scoped_attributes/version"
2
2
  require "scoped_attributes/scopable"
3
3
  require 'scoped_attributes/railtie' if defined?(Rails)
4
- require "active_support/concern"
4
+ require "active_support/all"
5
5
 
6
6
  module ScopedAttributes
7
7
  extend ActiveSupport::Concern
@@ -49,10 +49,6 @@ module ScopedAttributes
49
49
  end
50
50
  end
51
51
 
52
- def model
53
- (self.class.model_name || self.class.name.gsub("Scoped", "")).safe_constantize
54
- end
55
-
56
52
  def as_json
57
53
  attributes
58
54
  end
@@ -60,7 +56,7 @@ module ScopedAttributes
60
56
  def to_model
61
57
  return nil if model.nil?
62
58
 
63
- if object&.id.nil?
59
+ if object.try(:id).nil?
64
60
  model.new(attributes)
65
61
  else
66
62
  model.select(attributes.keys).find_by(id: object.id)
@@ -82,6 +78,9 @@ module ScopedAttributes
82
78
  end
83
79
 
84
80
  private
81
+ def model
82
+ (self.class.model_name || self.class.name.gsub("Scoped", "")).safe_constantize
83
+ end
85
84
 
86
85
  def visible?(only)
87
86
  return true if only.nil?
@@ -89,8 +88,8 @@ module ScopedAttributes
89
88
  case only
90
89
  when Array
91
90
  only.any? { |role| public_send("#{role}?") }
92
- when String
93
- only.public_send(string)
91
+ when Symbol
92
+ public_send(only)
94
93
  when Proc
95
94
  unbound_method = generate_method(:only_call, &only)
96
95
  !!unbound_method.bind(self).call
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scoped_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - h1kita
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-15 00:00:00.000000000 Z
11
+ date: 2019-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,6 +72,7 @@ executables: []
72
72
  extensions: []
73
73
  extra_rdoc_files: []
74
74
  files:
75
+ - ".github/workflows/ci.yml"
75
76
  - ".gitignore"
76
77
  - ".rspec"
77
78
  - ".travis.yml"