filterable_attributes 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d0bc0d20f36375af0299a062c2b3f27d7d9e2cac67be5eb390d6ad52f5e454e4
4
+ data.tar.gz: fbe4c52fb2de172685fa64450f50c5decee532acaaa97eca954ebbb4ec8c69b4
5
+ SHA512:
6
+ metadata.gz: 746296e7af7572b474f42323b478ff3243cf6e901f184d8b4f1909db4b1c7a20a41b27ce4268ea50268b6c8343e819f6094409552285546c414961531c89a377
7
+ data.tar.gz: 03a2a45f431223493fd766d16c9e01adae6dcb2c32f5e1248b457285558dd39b87064decb4d2b076772cc3e28aaf3df21731de85c286806eb81d4dbe4b8a286a
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
@@ -0,0 +1,5 @@
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Editor-based HTTP Client requests
5
+ /httpRequests/
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Settings><!--This file was automatically generated by Ruby plugin.
3
+ You are allowed to:
4
+ 1. Remove rake task
5
+ 2. Add existing rake tasks
6
+ To add existing rake tasks automatically delete this file and reload the project.
7
+ --><RakeGroup description="" fullCmd="" taksId="rake" /></Settings>
@@ -0,0 +1,17 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="ModuleRunConfigurationManager">
4
+ <shared />
5
+ </component>
6
+ <component name="NewModuleRootManager">
7
+ <content url="file://$MODULE_DIR$">
8
+ <sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
9
+ <sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
10
+ <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
11
+ </content>
12
+ <orderEntry type="inheritedJdk" />
13
+ <orderEntry type="sourceFolder" forTests="false" />
14
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v2.1.4, rbenv: 2.5.3) [gem]" level="application" />
15
+ <orderEntry type="library" scope="PROVIDED" name="rake (v12.3.0, rbenv: 2.5.3) [gem]" level="application" />
16
+ </component>
17
+ </module>
@@ -0,0 +1,6 @@
1
+ <component name="InspectionProjectProfileManager">
2
+ <profile version="1.0">
3
+ <option name="myName" value="Project Default" />
4
+ <inspection_tool class="Rubocop" enabled="false" level="WARNING" enabled_by_default="false" />
5
+ </profile>
6
+ </component>
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="JavaScriptSettings">
4
+ <option name="languageLevel" value="ES6" />
5
+ </component>
6
+ <component name="ProjectRootManager" version="2" project-jdk-name="rbenv: 2.5.3" project-jdk-type="RUBY_SDK" />
7
+ </project>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/filterable_attributes.iml" filepath="$PROJECT_DIR$/.idea/filterable_attributes.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.5
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at git@josh.mn. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in filterable_attributes.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,89 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ filterable_attributes (0.1.0)
5
+ active_model_serializers
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (6.0.3.2)
11
+ actionview (= 6.0.3.2)
12
+ activesupport (= 6.0.3.2)
13
+ rack (~> 2.0, >= 2.0.8)
14
+ rack-test (>= 0.6.3)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
17
+ actionview (6.0.3.2)
18
+ activesupport (= 6.0.3.2)
19
+ builder (~> 3.1)
20
+ erubi (~> 1.4)
21
+ rails-dom-testing (~> 2.0)
22
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
23
+ active_model_serializers (0.10.10)
24
+ actionpack (>= 4.1, < 6.1)
25
+ activemodel (>= 4.1, < 6.1)
26
+ case_transform (>= 0.2)
27
+ jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
28
+ activemodel (6.0.3.2)
29
+ activesupport (= 6.0.3.2)
30
+ activesupport (6.0.3.2)
31
+ concurrent-ruby (~> 1.0, >= 1.0.2)
32
+ i18n (>= 0.7, < 2)
33
+ minitest (~> 5.1)
34
+ tzinfo (~> 1.1)
35
+ zeitwerk (~> 2.2, >= 2.2.2)
36
+ builder (3.2.4)
37
+ case_transform (0.2)
38
+ activesupport
39
+ concurrent-ruby (1.1.6)
40
+ crass (1.0.6)
41
+ diff-lcs (1.4.4)
42
+ erubi (1.9.0)
43
+ i18n (1.8.3)
44
+ concurrent-ruby (~> 1.0)
45
+ jsonapi-renderer (0.2.2)
46
+ loofah (2.6.0)
47
+ crass (~> 1.0.2)
48
+ nokogiri (>= 1.5.9)
49
+ mini_portile2 (2.4.0)
50
+ minitest (5.14.1)
51
+ nokogiri (1.10.10)
52
+ mini_portile2 (~> 2.4.0)
53
+ rack (2.2.3)
54
+ rack-test (1.1.0)
55
+ rack (>= 1.0, < 3)
56
+ rails-dom-testing (2.0.3)
57
+ activesupport (>= 4.2.0)
58
+ nokogiri (>= 1.6)
59
+ rails-html-sanitizer (1.3.0)
60
+ loofah (~> 2.3)
61
+ rake (12.3.3)
62
+ rspec (3.9.0)
63
+ rspec-core (~> 3.9.0)
64
+ rspec-expectations (~> 3.9.0)
65
+ rspec-mocks (~> 3.9.0)
66
+ rspec-core (3.9.2)
67
+ rspec-support (~> 3.9.3)
68
+ rspec-expectations (3.9.2)
69
+ diff-lcs (>= 1.2.0, < 2.0)
70
+ rspec-support (~> 3.9.0)
71
+ rspec-mocks (3.9.1)
72
+ diff-lcs (>= 1.2.0, < 2.0)
73
+ rspec-support (~> 3.9.0)
74
+ rspec-support (3.9.3)
75
+ thread_safe (0.3.6)
76
+ tzinfo (1.2.7)
77
+ thread_safe (~> 0.1)
78
+ zeitwerk (2.3.1)
79
+
80
+ PLATFORMS
81
+ ruby
82
+
83
+ DEPENDENCIES
84
+ filterable_attributes!
85
+ rake (~> 12.0)
86
+ rspec (~> 3.0)
87
+
88
+ BUNDLED WITH
89
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Josh Brody
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,59 @@
1
+ # FilterableAttributes
2
+
3
+ When using [ActiveModelSerializers](https://github.com/rails-api/active_model_serializers), filter out attributes values instead of writing a ton of conditionals to change them.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ class ProductSerializer < ApplicationSerializer
9
+ include ActiveModelSerializers::FilterableAttributes
10
+ attributes :id, :price_in_cents
11
+ filter_attribute :price_in_cents, unless: :user_signed_in? do
12
+ "Sign in to see the price"
13
+ end
14
+
15
+ private
16
+ def user_signed_in?
17
+ scope.nil?
18
+ end
19
+ end
20
+
21
+ product = Product.find(params[:id])
22
+ render json: ProductSerializer.new(product, scope: current_user).to_json
23
+ ```
24
+
25
+ ## Installation
26
+
27
+ Add this line to your application's Gemfile:
28
+
29
+ ```ruby
30
+ gem 'filterable_attributes'
31
+ ```
32
+
33
+ And then execute:
34
+
35
+ $ bundle install
36
+
37
+ Or install it yourself as:
38
+
39
+ $ gem install filterable_attributes
40
+
41
+ ## Usage
42
+
43
+ 1. Add `include ActiveModelSerializers::FilterableAttributes` to your serializer
44
+ 2. Use `filter_attribute :price_in_cents` or `filter_attributes :price_in_cents, :tax_in_cents` followed by options
45
+ 3. Enjoy not having a mess of conditionals
46
+
47
+ ## Development
48
+
49
+ 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.
50
+
51
+ 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).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/joshmn/filterable_attributes. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/filterable_attributes/blob/master/CODE_OF_CONDUCT.md).
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "filterable_attributes"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/filterable_attributes/version'
5
+
6
+ version = FilterableAttributes::VERSION
7
+ system("gem build filterable_attributes.gemspec")
8
+ system("gem push filterable_attributes-#{version}.gem")
9
+ system("rm filterable_attributes-#{version}.gem")
10
+ system("git tag -a v#{version} -m 'version #{version}'")
11
+ system("git push origin v#{version}")
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/filterable_attributes/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "filterable_attributes"
5
+ spec.version = FilterableAttributes::VERSION
6
+ spec.authors = ["Josh Brody"]
7
+ spec.email = ["git@josh.mn"]
8
+
9
+ spec.summary = "Filterable attributes for ActiveModelSerializers."
10
+ spec.description = spec.summary
11
+ spec.homepage = "https://github.com/joshmn/filterable_attributes"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = spec.homepage
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+
27
+ spec.require_paths = ["lib"]
28
+ spec.add_dependency 'active_model_serializers'
29
+ end
@@ -0,0 +1,66 @@
1
+ require 'active_model_serializers'
2
+ require "filterable_attributes/version"
3
+
4
+ module ActiveModelSerializers
5
+ module FilterableAttributes
6
+ class ::ActiveModel::Serializer::Attribute
7
+ def value(serializer)
8
+ if filtered = serializer.class._filtered_attributes[name]
9
+ if filtered.excluded?(serializer)
10
+ super
11
+ else
12
+ filtered.value(serializer)
13
+ end
14
+ else
15
+ super
16
+ end
17
+ end
18
+ end
19
+
20
+ class FilteredAttribute < ActiveModel::Serializer::Attribute
21
+ def value(serializer)
22
+ if block
23
+ serializer.instance_eval(&block)
24
+ else
25
+ serializer.read_attribute_for_serialization(name)
26
+ end
27
+ end
28
+
29
+ def evaluate_condition(serializer)
30
+ case condition
31
+ when Symbol
32
+ serializer.send(condition)
33
+ when String
34
+ serializer.instance_eval(condition)
35
+ when Proc
36
+ if condition.arity.zero?
37
+ serializer.instance_exec(&condition)
38
+ else
39
+ serializer.instance_exec(serializer, &condition)
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ def self.included(klass)
46
+ klass.extend ClassMethods
47
+ end
48
+
49
+ module ClassMethods
50
+ def _filtered_attributes
51
+ @_filtered_attributes ||= {}
52
+ end
53
+
54
+ def filter_attribute(name, options = {}, &block)
55
+ _filtered_attributes[name] = FilteredAttribute.new(name, options, block)
56
+ end
57
+
58
+ def filter_attributes(*args, &block)
59
+ options = args.extract_options!
60
+ args.each do |name|
61
+ filter_attribute(name, options, &block)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module FilterableAttributes
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: filterable_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Brody
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_model_serializers
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Filterable attributes for ActiveModelSerializers.
28
+ email:
29
+ - git@josh.mn
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".idea/.gitignore"
36
+ - ".idea/.rakeTasks"
37
+ - ".idea/filterable_attributes.iml"
38
+ - ".idea/inspectionProfiles/Project_Default.xml"
39
+ - ".idea/misc.xml"
40
+ - ".idea/modules.xml"
41
+ - ".idea/vcs.xml"
42
+ - ".rspec"
43
+ - ".travis.yml"
44
+ - CODE_OF_CONDUCT.md
45
+ - Gemfile
46
+ - Gemfile.lock
47
+ - LICENSE.txt
48
+ - README.md
49
+ - Rakefile
50
+ - bin/console
51
+ - bin/release
52
+ - bin/setup
53
+ - filterable_attributes.gemspec
54
+ - lib/filterable_attributes.rb
55
+ - lib/filterable_attributes/version.rb
56
+ homepage: https://github.com/joshmn/filterable_attributes
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ allowed_push_host: https://rubygems.org
61
+ homepage_uri: https://github.com/joshmn/filterable_attributes
62
+ source_code_uri: https://github.com/joshmn/filterable_attributes
63
+ changelog_uri: https://github.com/joshmn/filterable_attributes
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 2.3.0
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 3.0.3
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Filterable attributes for ActiveModelSerializers.
83
+ test_files: []