fields-serializer 0.1.0

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: f29199a66db6d606d41313ebf962982b7bcfd4fc
4
+ data.tar.gz: a91564985913529e305c1fa82c929357d053cd5a
5
+ SHA512:
6
+ metadata.gz: 4c16d6c243b8946b8c18723d4bf4c03260cf568c750bc2fd6a5884a072650e185f72709b23516220975f9f3102620a104d83e52e5ce19bd7bbf7922879e6783b
7
+ data.tar.gz: c124dafada87956480064a427147700ed0356c5f276bcef654b478b9e59d22cefd6dc5d23796d239b39b8e3ea84c065aad5ab4e424aca372a20a21e63c8fe0d2
data/.gitignore ADDED
@@ -0,0 +1,102 @@
1
+ # See http://help.github.com/ignore-files/ for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Gems
11
+ /Gemfile.lock
12
+
13
+ # Ignore the default SQLite database.
14
+ /db/*.sqlite3
15
+ /db/*.sqlite3-journal
16
+
17
+ # Ignore gem building folder: pkg
18
+ pkg
19
+
20
+ # Ignore log and tmp files
21
+ log/*
22
+ *.log
23
+ tmp
24
+ tmp/**/*
25
+
26
+ # Documentation
27
+ /doc/
28
+ doc/api
29
+ doc/app
30
+ .yardoc
31
+ /_yardoc/
32
+ .yardopts
33
+
34
+ # Public Uploads
35
+ public/system/*
36
+ public/themes/*
37
+
38
+ # Public Cache
39
+ public/javascripts/cache
40
+ public/stylesheets/cache
41
+
42
+ # Vendor Cache
43
+ vendor/cache
44
+
45
+ # Acts as Indexed
46
+ index/**/*
47
+
48
+ # Refinery Specific
49
+ *.tmproj
50
+ *.autobackupbyrefinery.*
51
+ refinerycms-*.gem
52
+ .autotest
53
+
54
+ # Mac
55
+ .DS_Store
56
+
57
+ # Windows
58
+ Thumbs.db
59
+
60
+ # NetBeans
61
+ nbproject
62
+
63
+ # Eclipse
64
+ .project
65
+
66
+ # Redcar
67
+ .redcar
68
+
69
+ # Rubinius
70
+ *.rbc
71
+
72
+ # RVM / rbenv
73
+ .ruby-version
74
+ .ruby-gemset
75
+ .rvmrc
76
+
77
+ # Vim
78
+ *.swp
79
+ *.swo
80
+
81
+ # RubyMine
82
+ .idea
83
+
84
+ # E-texteditor
85
+ .eprj
86
+
87
+ # Backup
88
+ *~
89
+
90
+ # Capybara Bug
91
+ capybara-*html
92
+
93
+ # sass
94
+ .sass-cache
95
+ .sass-cache/*
96
+
97
+ # SimpleCov (tests code converage)
98
+ /coverage
99
+
100
+ # RSpec
101
+ /spec/reports/
102
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --require byebug
3
+ --require spec_helper
4
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.4
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in fields-serializer.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Lorenzo Tello
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.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # Fields::Serializer
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fields/serializer`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'fields-serializer'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install fields-serializer
22
+
23
+ ## Usage
24
+
25
+ Use example in your controller:
26
+
27
+ ```ruby
28
+ class OrdersController < ApplicationController
29
+ def index
30
+ orders = Order.all
31
+ render_json_fields(orders, model_class: Order, fields: ["short_id", "customer.first_name", "customer.surname", "customer.address.postcode"], root: "orders")
32
+ end
33
+ end
34
+
35
+ ```
36
+
37
+ If `:model_class` and `:fields` options are included, it will optimize the query to the db using `.includes` based
38
+ on the model_class associations found in `:fields` value. Also, will render only the fields in `:fields` option.
39
+
40
+ If no :model_class and `:fields` options are provided, it will render the whole objects using the appropriate model
41
+ serializer.
42
+
43
+ You can also provide any other options accepted by common `render` method.
44
+
45
+ ## Development
46
+
47
+ 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.
48
+
49
+ 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).
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ltello/fields-serializer.
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fields/serializer"
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__)
data/bin/setup ADDED
@@ -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,33 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "fields/serializer/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fields-serializer"
8
+ spec.version = Fields::Serializer::VERSION
9
+ spec.authors = ["Stuart Chinery", "Miguel Montalbo", "Lorenzo Tello"]
10
+ spec.email = ["stuart.chinery@gmail.com, mmontalbo@zonedigital.com, ltello8a@gmail.com"]
11
+
12
+ spec.summary = "Extensions to ActiveRecord and ActionController to serialize a subset of model fields"
13
+ spec.description = "Extensions to ActiveRecord and ActionController to serialize a subset of model fields"
14
+ spec.homepage = "https://github.com/ltello/fields-serializer"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.6"
27
+ spec.add_development_dependency "factory_girl", "~> 4.8"
28
+ spec.add_development_dependency "byebug", "~> 5.0"
29
+ spec.add_development_dependency "simplecov"
30
+
31
+ spec.add_runtime_dependency "rails", "~> 5.0"
32
+ spec.add_runtime_dependency "active_model_serializers", "~> 0.10"
33
+ end
@@ -0,0 +1,27 @@
1
+ require 'action_controller'
2
+
3
+ module Fields
4
+ module Serializer
5
+ module ActionController
6
+ extend ActiveSupport::Concern
7
+
8
+
9
+ # Render the result of an ActiveRecord query including only the fields specified if any
10
+ # or the whole serialized objects.
11
+ #
12
+ # @param [ActiveRecord_Relation] query - The query to render in json
13
+ # @option options [Array] :fields - The list of fields to return in json api syntax
14
+ # @option options [Class] :model_class - The model class of the objects to be queried to optimize db hits.
15
+ # @option options [Hash] :options - Any other valid option to render method.
16
+ def render_json_fields(query, **options)
17
+ fields = options.delete(:fields)
18
+ model_class = options.delete(:model_class)
19
+ if fields
20
+ query = query.includes(*model_class.fields_to_includes(fields))
21
+ options.merge!(each_serializer: FieldSerializer, fields: Array(fields))
22
+ end
23
+ render options.merge!(json: query.to_a)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,36 @@
1
+ require 'active_record'
2
+
3
+ module Fields
4
+ module Serializer
5
+ module ActiveRecord
6
+ extend ActiveSupport::Concern
7
+
8
+ class_methods do
9
+ def fields_to_includes(fields)
10
+ flatten_fields = Array(fields).map { |str| str.to_s.split(",").map(&:strip) }.flatten
11
+ nested_fields = flatten_fields.map { |field| nested_field(field.split(".")) }.compact
12
+ nested_fields.inject([{}]) do |result, attribute_structure|
13
+ if attribute_structure.is_a?(Hash)
14
+ result.first.deep_merge!(attribute_structure) { |_, u, v| [u, v] } && result
15
+ else
16
+ result << attribute_structure
17
+ end
18
+ end.map(&:presence).compact
19
+ end
20
+
21
+ private
22
+
23
+ def association?(key)
24
+ reflections.keys.include?(key)
25
+ end
26
+
27
+ def nested_field(attribute_stack)
28
+ parent = attribute_stack.first
29
+ return unless association?(parent)
30
+ parent_klass = reflections[parent].class_name.constantize
31
+ { parent => parent_klass.nested_field(attribute_stack[1..-1]) }.compact.presence || parent
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,39 @@
1
+ require "active_model_serializers"
2
+
3
+ # This is a generic serializer intended to return a subset of a model's attributes.
4
+ # It can be used with any model but does not currently support associations.
5
+ #
6
+ # Example usage:
7
+ # `render json: @region, serializer: FieldSerializer, fields: [:id, :title]`
8
+ #
9
+ # > { "id": "5f19582d-ee28-4e89-9e3a-edc42a8b59e5", "title": "London" }
10
+
11
+ class FieldSerializer < ActiveModel::Serializer
12
+ def attributes(*args)
13
+ adding_id do
14
+ merging_attributes do
15
+ args.first.map { |field| create_attribute_structure(field.split("."), object) }
16
+ end
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def adding_id(&block)
23
+ block.call.merge(id: object.id)
24
+ end
25
+
26
+ def create_attribute_structure(attribute_stack, model)
27
+ parent = attribute_stack.shift
28
+ if attribute_stack.count > 0
29
+ nested_model = model.send(parent)
30
+ { parent => create_attribute_structure(attribute_stack, nested_model) }
31
+ else
32
+ { parent => model.send(parent) }
33
+ end
34
+ end
35
+
36
+ def merging_attributes(&block)
37
+ block.call.inject(:deep_merge!)
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ module Fields
2
+ module Serializer
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext'
3
+ require 'active_support/concern'
4
+ require "fields/serializer/version"
5
+ require "fields/serializer/active_record"
6
+ require "fields/serializer/action_controller"
7
+ require "fields/serializer/field_serializer"
8
+
9
+ module Fields
10
+ module Serializer
11
+
12
+ ActiveSupport.on_load(:active_record) do
13
+ include Fields::Serializer::ActiveRecord
14
+ end
15
+
16
+ ActiveSupport.on_load(:action_controller) do
17
+ include Fields::Serializer::ActionController
18
+ end
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fields-serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stuart Chinery
8
+ - Miguel Montalbo
9
+ - Lorenzo Tello
10
+ autorequire:
11
+ bindir: exe
12
+ cert_chain: []
13
+ date: 2017-12-14 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.16'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '1.16'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rake
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '10.0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '10.0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: rspec
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '3.6'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '3.6'
57
+ - !ruby/object:Gem::Dependency
58
+ name: factory_girl
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '4.8'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '4.8'
71
+ - !ruby/object:Gem::Dependency
72
+ name: byebug
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '5.0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - "~>"
83
+ - !ruby/object:Gem::Version
84
+ version: '5.0'
85
+ - !ruby/object:Gem::Dependency
86
+ name: simplecov
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ - !ruby/object:Gem::Dependency
100
+ name: rails
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - "~>"
104
+ - !ruby/object:Gem::Version
105
+ version: '5.0'
106
+ type: :runtime
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - "~>"
111
+ - !ruby/object:Gem::Version
112
+ version: '5.0'
113
+ - !ruby/object:Gem::Dependency
114
+ name: active_model_serializers
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: '0.10'
120
+ type: :runtime
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - "~>"
125
+ - !ruby/object:Gem::Version
126
+ version: '0.10'
127
+ description: Extensions to ActiveRecord and ActionController to serialize a subset
128
+ of model fields
129
+ email:
130
+ - stuart.chinery@gmail.com, mmontalbo@zonedigital.com, ltello8a@gmail.com
131
+ executables: []
132
+ extensions: []
133
+ extra_rdoc_files: []
134
+ files:
135
+ - ".gitignore"
136
+ - ".rspec"
137
+ - ".travis.yml"
138
+ - Gemfile
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - fields-serializer.gemspec
145
+ - lib/fields/serializer.rb
146
+ - lib/fields/serializer/action_controller.rb
147
+ - lib/fields/serializer/active_record.rb
148
+ - lib/fields/serializer/field_serializer.rb
149
+ - lib/fields/serializer/version.rb
150
+ homepage: https://github.com/ltello/fields-serializer
151
+ licenses:
152
+ - MIT
153
+ metadata: {}
154
+ post_install_message:
155
+ rdoc_options: []
156
+ require_paths:
157
+ - lib
158
+ required_ruby_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ required_rubygems_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ requirements: []
169
+ rubyforge_project:
170
+ rubygems_version: 2.6.13
171
+ signing_key:
172
+ specification_version: 4
173
+ summary: Extensions to ActiveRecord and ActionController to serialize a subset of
174
+ model fields
175
+ test_files: []