permittribute 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +103 -0
- data/Rakefile +2 -0
- data/lib/generators/permittribute/install_generator.rb +21 -0
- data/lib/generators/permittribute/templates/admin.rb +12 -0
- data/lib/generators/permittribute/templates/api.rb +12 -0
- data/lib/generators/permittribute/templates/default.rb +12 -0
- data/lib/permittribute.rb +13 -0
- data/lib/permittribute/attrs.rb +45 -0
- data/lib/permittribute/engine.rb +20 -0
- data/lib/permittribute/strong_params.rb +9 -0
- data/lib/permittribute/version.rb +3 -0
- data/permittribute.gemspec +23 -0
- metadata +94 -0
data/.gitignore
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
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
|
|
18
|
+
*.bundle
|
|
19
|
+
*.so
|
|
20
|
+
*.o
|
|
21
|
+
*.a
|
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2015 Priyank Gupta
|
|
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,103 @@
|
|
|
1
|
+
# Permittribute
|
|
2
|
+
|
|
3
|
+
We sometimes face scenarios where we need same permitted attributes for a model in different controllers and end up re-writing all attributes again (violating DRY).
|
|
4
|
+
|
|
5
|
+
Permittribute is meant to:
|
|
6
|
+
|
|
7
|
+
- reuse same permitted attributes at different locations/controllers.
|
|
8
|
+
- scope permitted attributes base on roles like admin, api etc.
|
|
9
|
+
- group permitted attributes at a single reference point in their respective scopes.
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
This is used with <a href='https://github.com/rails/strong_parameters'>rails/strong_parameters</a> which is now incorporated since rails 4.x.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
We use strong parameters something like:
|
|
17
|
+
|
|
18
|
+
# in controllers/articles_controller.rb
|
|
19
|
+
def article_params
|
|
20
|
+
params.require(:article).permit([:title, :author_id, :published_at, :publication])
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# in controllers/admin/articles_controller.rb
|
|
24
|
+
def article_params
|
|
25
|
+
params.require(:article).permit([:title, :author_id, :published_at, :publication])
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# in controllers/api/articles_controller.rb
|
|
29
|
+
def article_params
|
|
30
|
+
params.require(:article).permit([:title, :author_id, :published_at, :publication])
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
With <b>Permittribute</b> we can do:
|
|
34
|
+
|
|
35
|
+
# in lib/permittribute/default.rb
|
|
36
|
+
Permittribute.configure do
|
|
37
|
+
config.articles = [:title, :author_id, :published_at, :publication]
|
|
38
|
+
# Use as #permittribute_articles
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# in lib/permittribute/admin.rb
|
|
42
|
+
Permittribute.configure(:admin) do
|
|
43
|
+
config.articles = [:title, :author_id, :published_at, :publication]
|
|
44
|
+
# Use as #permittribute_admin_articles
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# in lib/permittribute/api.rb
|
|
48
|
+
Permittribute.configure(:api) do
|
|
49
|
+
config.articles = [:title, :author_id, :published_at, :publication]
|
|
50
|
+
# Use as #permittribute_api_articles
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
And
|
|
54
|
+
|
|
55
|
+
# in controllers/articles_controller.rb
|
|
56
|
+
def article_params
|
|
57
|
+
params.require(:article).permit(permittribute_articles)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# in controllers/admin/articles_controller.rb
|
|
61
|
+
def article_params
|
|
62
|
+
params.require(:article).permit(permittribute_admin_articles)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# in controllers/api/articles_controller.rb
|
|
66
|
+
def article_params
|
|
67
|
+
params.require(:article).permit(permittribute_api_articles)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
## Getting Started
|
|
71
|
+
|
|
72
|
+
Add this line to your application's Gemfile:
|
|
73
|
+
|
|
74
|
+
gem 'permittribute'
|
|
75
|
+
|
|
76
|
+
And then execute:
|
|
77
|
+
|
|
78
|
+
$ bundle
|
|
79
|
+
|
|
80
|
+
Or install it yourself as:
|
|
81
|
+
|
|
82
|
+
$ gem install permittribute
|
|
83
|
+
|
|
84
|
+
After you have installed Permittribute or added it to your Gemfile, you need to run the generator:
|
|
85
|
+
|
|
86
|
+
$ rails generate permittribute:install
|
|
87
|
+
|
|
88
|
+
This will
|
|
89
|
+
|
|
90
|
+
- create three configurations file, `lib/permittribute/default.rb`, `lib/permittribute/admin.rb` and `lib/permittribute/api.rb`. These contains some example configurations.
|
|
91
|
+
- adds `config.eager_load_paths += ["#{Rails.root}/lib/permittribute"]` in `config/application.rb`. To eager load created config files.
|
|
92
|
+
|
|
93
|
+
## TODO
|
|
94
|
+
|
|
95
|
+
- Add specs.
|
|
96
|
+
|
|
97
|
+
## Contributing
|
|
98
|
+
|
|
99
|
+
1. Fork it ( https://github.com/[my-github-username]/permittribute/fork )
|
|
100
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
101
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
102
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
103
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'rails/generators'
|
|
2
|
+
module Permittribute
|
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
|
4
|
+
def self.source_root
|
|
5
|
+
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def configure_application
|
|
9
|
+
application <<-APP
|
|
10
|
+
|
|
11
|
+
config.eager_load_paths += ["\#{Rails.root}/lib/permittribute"]
|
|
12
|
+
APP
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def generate_config
|
|
16
|
+
template "default.rb", "lib/permittribute/default.rb"
|
|
17
|
+
template "admin.rb", "lib/permittribute/admin.rb"
|
|
18
|
+
template "api.rb", "lib/permittribute/api.rb"
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Permittribute.configure(:admin) do |config|
|
|
2
|
+
# add class variables for permitted attributes
|
|
3
|
+
# For example:
|
|
4
|
+
# config.articles = [:title, :summary, :author_id, :published_at, :publication]
|
|
5
|
+
# Use as #permittribute_admin_articles
|
|
6
|
+
|
|
7
|
+
# config.books = [:title, :summary, :author_id, :published_at, :publication, :price, :status]
|
|
8
|
+
# Use as #permittribute_admin_books
|
|
9
|
+
|
|
10
|
+
# config.authors = [:name, :language, :age]
|
|
11
|
+
# Use as #permittribute_admin_authors
|
|
12
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Permittribute.configure(:api) do |config|
|
|
2
|
+
# add class variables for permitted attributes
|
|
3
|
+
# For example:
|
|
4
|
+
# config.articles = [:title, :summary, :author_id, :published_at, :publication]
|
|
5
|
+
# Use as #permittribute_api_articles
|
|
6
|
+
|
|
7
|
+
# config.books = [:title, :summary, :author_id, :published_at, :publication, :price, :status]
|
|
8
|
+
# Use as #permittribute_api_books
|
|
9
|
+
|
|
10
|
+
# config.authors = [:name, :language, :age]
|
|
11
|
+
# Use as #permittribute_api_authors
|
|
12
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Permittribute.configure do |config|
|
|
2
|
+
# add class variables for permitted attributes
|
|
3
|
+
# For example:
|
|
4
|
+
# config.articles = [:title, :summary, :author_id, :published_at, :publication]
|
|
5
|
+
# Use as #permittribute_articles
|
|
6
|
+
|
|
7
|
+
# config.books = [:title, :summary, :author_id, :published_at, :publication, :price, :status]
|
|
8
|
+
# Use as #permittribute_books
|
|
9
|
+
|
|
10
|
+
# config.authors = [:name, :language, :age]
|
|
11
|
+
# Use as #permittribute_authors
|
|
12
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
require "permittribute/version"
|
|
2
|
+
require 'permittribute/strong_params'
|
|
3
|
+
|
|
4
|
+
require 'permittribute/engine' if defined?(Rails)
|
|
5
|
+
|
|
6
|
+
module Permittribute
|
|
7
|
+
def self.configure(role=:default, &block)
|
|
8
|
+
Permittribute::Attrs.configure_with_role(role.to_sym, &block)
|
|
9
|
+
|
|
10
|
+
Permittribute::StrongParams.delegate *Permittribute::Attrs.defined_meth_names_for_role(role),
|
|
11
|
+
to: :permittribute, prefix: :permittribute
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
module Permittribute
|
|
2
|
+
class Attrs
|
|
3
|
+
@@attributes_group_by_roles = {}
|
|
4
|
+
@@defined_meth_names = Hash.new {|k,v| k[v] = []}
|
|
5
|
+
|
|
6
|
+
class << self
|
|
7
|
+
def all
|
|
8
|
+
@@defined_meth_names.values.flatten
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def defined_meth_names_for_role(role)
|
|
12
|
+
@@defined_meth_names[role] || []
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def configure_with_role(role, &block)
|
|
16
|
+
@@attributes_group_by_roles[role] ||= OpenStruct.new
|
|
17
|
+
|
|
18
|
+
yield @@attributes_group_by_roles[role]
|
|
19
|
+
|
|
20
|
+
define_role_attribute_methods(role)
|
|
21
|
+
@@defined_meth_names[role].uniq!
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def define_role_attribute_methods(role)
|
|
25
|
+
@@attributes_group_by_roles[role].each_pair do |attribute, params|
|
|
26
|
+
define_role_attribute_method(role, attribute, params)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def define_role_attribute_method(role, attribute, params)
|
|
31
|
+
meth_name = (role == :default ? attribute : "#{role}_#{attribute}").to_sym
|
|
32
|
+
unless respond_to? meth_name
|
|
33
|
+
instance_eval <<-eometh
|
|
34
|
+
def #{meth_name}
|
|
35
|
+
@@attributes_group_by_roles['#{role}'.to_sym].send('#{attribute}'.to_sym)
|
|
36
|
+
end
|
|
37
|
+
eometh
|
|
38
|
+
|
|
39
|
+
@@defined_meth_names[role] << meth_name
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module Permittribute
|
|
2
|
+
class Engine < ::Rails::Engine
|
|
3
|
+
|
|
4
|
+
engine_name "permittribute"
|
|
5
|
+
|
|
6
|
+
initializer 'permittribute.add_strong_params' do |app|
|
|
7
|
+
ActiveSupport.on_load :action_controller do
|
|
8
|
+
include Permittribute::StrongParams
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
initializer "permittribute.configure", group: :all do |app|
|
|
13
|
+
Dir[Rails.root.join('lib', 'permittribute/*.rb').to_s].each do |c|
|
|
14
|
+
app.config.to_prepare do
|
|
15
|
+
Rails.configuration.cache_classes ? require(c) : load(c)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'permittribute/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "permittribute"
|
|
8
|
+
spec.version = Permittribute::VERSION
|
|
9
|
+
spec.authors = ["Priyank Gupta"]
|
|
10
|
+
spec.email = ["priyankgupta1988@gmail.com", "priyankgupta1988@yahoo.com"]
|
|
11
|
+
spec.summary = %q{ Reuse Permitted Attributes and group them at a single reference file }
|
|
12
|
+
spec.description = spec.summary
|
|
13
|
+
spec.homepage = ""
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
|
+
spec.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
|
22
|
+
spec.add_development_dependency "rake"
|
|
23
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: permittribute
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Priyank Gupta
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2015-08-02 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: bundler
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ~>
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '1.6'
|
|
22
|
+
type: :development
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ~>
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '1.6'
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: rake
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
33
|
+
none: false
|
|
34
|
+
requirements:
|
|
35
|
+
- - ! '>='
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
type: :development
|
|
39
|
+
prerelease: false
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ! '>='
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0'
|
|
46
|
+
description: Reuse Permitted Attributes and group them at a single reference file
|
|
47
|
+
email:
|
|
48
|
+
- priyankgupta1988@gmail.com
|
|
49
|
+
- priyankgupta1988@yahoo.com
|
|
50
|
+
executables: []
|
|
51
|
+
extensions: []
|
|
52
|
+
extra_rdoc_files: []
|
|
53
|
+
files:
|
|
54
|
+
- .gitignore
|
|
55
|
+
- Gemfile
|
|
56
|
+
- LICENSE.txt
|
|
57
|
+
- README.md
|
|
58
|
+
- Rakefile
|
|
59
|
+
- lib/generators/permittribute/install_generator.rb
|
|
60
|
+
- lib/generators/permittribute/templates/admin.rb
|
|
61
|
+
- lib/generators/permittribute/templates/api.rb
|
|
62
|
+
- lib/generators/permittribute/templates/default.rb
|
|
63
|
+
- lib/permittribute.rb
|
|
64
|
+
- lib/permittribute/attrs.rb
|
|
65
|
+
- lib/permittribute/engine.rb
|
|
66
|
+
- lib/permittribute/strong_params.rb
|
|
67
|
+
- lib/permittribute/version.rb
|
|
68
|
+
- permittribute.gemspec
|
|
69
|
+
homepage: ''
|
|
70
|
+
licenses:
|
|
71
|
+
- MIT
|
|
72
|
+
post_install_message:
|
|
73
|
+
rdoc_options: []
|
|
74
|
+
require_paths:
|
|
75
|
+
- lib
|
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
77
|
+
none: false
|
|
78
|
+
requirements:
|
|
79
|
+
- - ! '>='
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
|
+
none: false
|
|
84
|
+
requirements:
|
|
85
|
+
- - ! '>='
|
|
86
|
+
- !ruby/object:Gem::Version
|
|
87
|
+
version: '0'
|
|
88
|
+
requirements: []
|
|
89
|
+
rubyforge_project:
|
|
90
|
+
rubygems_version: 1.8.23
|
|
91
|
+
signing_key:
|
|
92
|
+
specification_version: 3
|
|
93
|
+
summary: Reuse Permitted Attributes and group them at a single reference file
|
|
94
|
+
test_files: []
|