active_model_serializers 0.9.8 → 0.10.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +21 -0
- data/.travis.yml +27 -0
- data/CHANGELOG.md +8 -200
- data/CONTRIBUTING.md +23 -12
- data/Gemfile +17 -0
- data/{MIT-LICENSE → LICENSE.txt} +3 -2
- data/README.md +151 -781
- data/Rakefile +12 -0
- data/active_model_serializers.gemspec +26 -0
- data/lib/action_controller/serialization.rb +30 -75
- data/lib/active_model/serializer.rb +192 -252
- data/lib/active_model/serializer/adapter.rb +87 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +78 -0
- data/lib/active_model/serializer/adapter/json.rb +52 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +15 -0
- data/lib/active_model/serializer/adapter/json_api.rb +152 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +22 -0
- data/lib/active_model/serializer/adapter/null.rb +11 -0
- data/lib/active_model/serializer/array_serializer.rb +32 -0
- data/lib/active_model/serializer/configuration.rb +13 -0
- data/lib/active_model/serializer/fieldset.rb +40 -0
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +5 -7
- data/lib/generators/serializer/USAGE +6 -0
- data/lib/{active_model/serializer/generators → generators}/serializer/serializer_generator.rb +8 -8
- data/lib/{active_model/serializer/generators → generators}/serializer/templates/serializer.rb +2 -2
- data/test/action_controller/adapter_selector_test.rb +51 -0
- data/test/action_controller/explicit_serializer_test.rb +110 -0
- data/test/action_controller/json_api_linked_test.rb +173 -0
- data/test/action_controller/serialization_scope_name_test.rb +63 -0
- data/test/action_controller/serialization_test.rb +365 -0
- data/test/adapter/fragment_cache_test.rb +27 -0
- data/test/adapter/json/belongs_to_test.rb +41 -0
- data/test/adapter/json/collection_test.rb +59 -0
- data/test/adapter/json/has_many_test.rb +36 -0
- data/test/adapter/json_api/belongs_to_test.rb +147 -0
- data/test/adapter/json_api/collection_test.rb +89 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +106 -0
- data/test/adapter/json_api/has_one_test.rb +59 -0
- data/test/adapter/json_api/linked_test.rb +257 -0
- data/test/adapter/json_test.rb +34 -0
- data/test/adapter/null_test.rb +25 -0
- data/test/adapter_test.rb +43 -0
- data/test/array_serializer_test.rb +29 -0
- data/test/fixtures/poro.rb +125 -142
- data/test/serializers/adapter_for_test.rb +50 -0
- data/test/serializers/associations_test.rb +106 -0
- data/test/serializers/attribute_test.rb +23 -0
- data/test/serializers/attributes_test.rb +28 -0
- data/test/serializers/cache_test.rb +128 -0
- data/test/serializers/configuration_test.rb +15 -0
- data/test/serializers/fieldset_test.rb +26 -0
- data/test/serializers/generators_test.rb +59 -0
- data/test/serializers/meta_test.rb +78 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/serializer_for_test.rb +56 -0
- data/test/serializers/urls_test.rb +26 -0
- data/test/test_helper.rb +21 -4
- metadata +121 -159
- data/DESIGN.textile +0 -586
- data/lib/action_controller/serialization_test_case.rb +0 -79
- data/lib/active_model/array_serializer.rb +0 -68
- data/lib/active_model/default_serializer.rb +0 -28
- data/lib/active_model/serializable.rb +0 -59
- data/lib/active_model/serializable/utils.rb +0 -16
- data/lib/active_model/serializer/association.rb +0 -58
- data/lib/active_model/serializer/association/has_many.rb +0 -39
- data/lib/active_model/serializer/association/has_one.rb +0 -25
- data/lib/active_model/serializer/config.rb +0 -31
- data/lib/active_model/serializer/generators/resource_override.rb +0 -13
- data/lib/active_model/serializer/generators/serializer/USAGE +0 -9
- data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +0 -14
- data/lib/active_model/serializer/generators/serializer/templates/controller.rb +0 -93
- data/lib/active_model/serializer/railtie.rb +0 -22
- data/lib/active_model/serializer_support.rb +0 -5
- data/lib/active_model_serializers/model/caching.rb +0 -25
- data/test/benchmark/app.rb +0 -60
- data/test/benchmark/benchmarking_support.rb +0 -67
- data/test/benchmark/bm_active_record.rb +0 -41
- data/test/benchmark/setup.rb +0 -75
- data/test/benchmark/tmp/miniprofiler/mp_timers_6eqewtfgrhitvq5gqm25 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_8083sx03hu72pxz1a4d0 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_fyz2gsml4z0ph9kpoy1c +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_hjry5rc32imd42oxoi48 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_m8fpoz2cvt3g9agz0bs3 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_p92m2drnj1i568u3sta0 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_qg52tpca3uesdfguee9i +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_s15t1a6mvxe0z7vjv790 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_x8kal3d17nfds6vp4kcj +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_views_127.0.0.1 +0 -0
- data/test/fixtures/active_record.rb +0 -96
- data/test/fixtures/template.html.erb +0 -1
- data/test/integration/action_controller/namespaced_serialization_test.rb +0 -105
- data/test/integration/action_controller/serialization_test.rb +0 -287
- data/test/integration/action_controller/serialization_test_case_test.rb +0 -71
- data/test/integration/active_record/active_record_test.rb +0 -94
- data/test/integration/generators/resource_generator_test.rb +0 -26
- data/test/integration/generators/scaffold_controller_generator_test.rb +0 -64
- data/test/integration/generators/serializer_generator_test.rb +0 -41
- data/test/test_app.rb +0 -14
- data/test/tmp/app/assets/javascripts/accounts.js +0 -2
- data/test/tmp/app/assets/stylesheets/accounts.css +0 -4
- data/test/tmp/app/controllers/accounts_controller.rb +0 -2
- data/test/tmp/app/helpers/accounts_helper.rb +0 -2
- data/test/tmp/app/serializers/account_serializer.rb +0 -3
- data/test/tmp/config/routes.rb +0 -1
- data/test/unit/active_model/array_serializer/except_test.rb +0 -18
- data/test/unit/active_model/array_serializer/key_format_test.rb +0 -18
- data/test/unit/active_model/array_serializer/meta_test.rb +0 -53
- data/test/unit/active_model/array_serializer/only_test.rb +0 -18
- data/test/unit/active_model/array_serializer/options_test.rb +0 -16
- data/test/unit/active_model/array_serializer/root_test.rb +0 -102
- data/test/unit/active_model/array_serializer/scope_test.rb +0 -24
- data/test/unit/active_model/array_serializer/serialization_test.rb +0 -216
- data/test/unit/active_model/default_serializer_test.rb +0 -13
- data/test/unit/active_model/serializer/associations/build_serializer_test.rb +0 -36
- data/test/unit/active_model/serializer/associations_test.rb +0 -49
- data/test/unit/active_model/serializer/attributes_test.rb +0 -57
- data/test/unit/active_model/serializer/config_test.rb +0 -91
- data/test/unit/active_model/serializer/filter_test.rb +0 -69
- data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +0 -189
- data/test/unit/active_model/serializer/has_many_test.rb +0 -265
- data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +0 -27
- data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +0 -196
- data/test/unit/active_model/serializer/has_one_test.rb +0 -253
- data/test/unit/active_model/serializer/key_format_test.rb +0 -25
- data/test/unit/active_model/serializer/meta_test.rb +0 -39
- data/test/unit/active_model/serializer/options_test.rb +0 -42
- data/test/unit/active_model/serializer/root_test.rb +0 -117
- data/test/unit/active_model/serializer/scope_test.rb +0 -49
- data/test/unit/active_model/serializer/url_helpers_test.rb +0 -35
- data/test/unit/active_model/serilizable_test.rb +0 -50
@@ -1,93 +0,0 @@
|
|
1
|
-
<% if namespaced? -%>
|
2
|
-
require_dependency "<%= namespaced_file_path %>/application_controller"
|
3
|
-
|
4
|
-
<% end -%>
|
5
|
-
<% module_namespacing do -%>
|
6
|
-
class <%= controller_class_name %>Controller < ApplicationController
|
7
|
-
before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
|
8
|
-
|
9
|
-
# GET <%= route_url %>
|
10
|
-
# GET <%= route_url %>.json
|
11
|
-
def index
|
12
|
-
@<%= plural_table_name %> = <%= orm_class.all(class_name) %>
|
13
|
-
|
14
|
-
respond_to do |format|
|
15
|
-
format.html # index.html.erb
|
16
|
-
format.json { render json: <%= "@#{plural_table_name}" %> }
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
# GET <%= route_url %>/1
|
21
|
-
# GET <%= route_url %>/1.json
|
22
|
-
def show
|
23
|
-
respond_to do |format|
|
24
|
-
format.html # show.html.erb
|
25
|
-
format.json { render json: <%= "@#{singular_table_name}" %> }
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
# GET <%= route_url %>/new
|
30
|
-
def new
|
31
|
-
@<%= singular_table_name %> = <%= orm_class.build(class_name) %>
|
32
|
-
end
|
33
|
-
|
34
|
-
# GET <%= route_url %>/1/edit
|
35
|
-
def edit
|
36
|
-
end
|
37
|
-
|
38
|
-
# POST <%= route_url %>
|
39
|
-
# POST <%= route_url %>.json
|
40
|
-
def create
|
41
|
-
@<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
|
42
|
-
|
43
|
-
respond_to do |format|
|
44
|
-
if @<%= orm_instance.save %>
|
45
|
-
format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %> }
|
46
|
-
format.json { render json: <%= "@#{singular_table_name}" %>, status: :created }
|
47
|
-
else
|
48
|
-
format.html { render action: 'new' }
|
49
|
-
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
# PATCH/PUT <%= route_url %>/1
|
55
|
-
# PATCH/PUT <%= route_url %>/1.json
|
56
|
-
def update
|
57
|
-
respond_to do |format|
|
58
|
-
if @<%= orm_instance.update("#{singular_table_name}_params") %>
|
59
|
-
format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> }
|
60
|
-
format.json { head :no_content }
|
61
|
-
else
|
62
|
-
format.html { render action: 'edit' }
|
63
|
-
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
# DELETE <%= route_url %>/1
|
69
|
-
# DELETE <%= route_url %>/1.json
|
70
|
-
def destroy
|
71
|
-
@<%= orm_instance.destroy %>
|
72
|
-
respond_to do |format|
|
73
|
-
format.html { redirect_to <%= index_helper %>_url }
|
74
|
-
format.json { head :no_content }
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
private
|
79
|
-
# Use callbacks to share common setup or constraints between actions.
|
80
|
-
def set_<%= singular_table_name %>
|
81
|
-
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
82
|
-
end
|
83
|
-
|
84
|
-
# Never trust parameters from the scary internet, only allow the white list through.
|
85
|
-
def <%= "#{singular_table_name}_params" %>
|
86
|
-
<%- if attributes_names.empty? -%>
|
87
|
-
params[<%= ":#{singular_table_name}" %>]
|
88
|
-
<%- else -%>
|
89
|
-
params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
|
90
|
-
<%- end -%>
|
91
|
-
end
|
92
|
-
end
|
93
|
-
<% end -%>
|
@@ -1,22 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
class Railtie < Rails::Railtie
|
3
|
-
initializer 'generators' do |app|
|
4
|
-
app.load_generators
|
5
|
-
require 'active_model/serializer/generators/serializer/serializer_generator'
|
6
|
-
require 'active_model/serializer/generators/serializer/scaffold_controller_generator'
|
7
|
-
require 'active_model/serializer/generators/resource_override'
|
8
|
-
end
|
9
|
-
|
10
|
-
initializer 'include_routes.active_model_serializer' do |app|
|
11
|
-
ActiveSupport.on_load(:active_model_serializers) do
|
12
|
-
include app.routes.url_helpers
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
config.to_prepare do
|
17
|
-
ActiveModel::Serializer.serializers_cache.clear
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModel::Serializer)
|
@@ -1,25 +0,0 @@
|
|
1
|
-
module ActiveModelSerializers
|
2
|
-
class Model
|
3
|
-
module Caching
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
included do
|
7
|
-
attr_writer :updated_at
|
8
|
-
attributes :id
|
9
|
-
end
|
10
|
-
|
11
|
-
# Defaults to the downcased model name and updated_at
|
12
|
-
def cache_key
|
13
|
-
ActiveSupport::Cache.expand_cache_key([
|
14
|
-
self.class.model_name.name.downcase,
|
15
|
-
"#{id}-#{updated_at.strftime('%Y%m%d%H%M%S%9N')}"
|
16
|
-
].compact)
|
17
|
-
end
|
18
|
-
|
19
|
-
# Defaults to the time the serializer file was modified.
|
20
|
-
def updated_at
|
21
|
-
defined?(@updated_at) ? @updated_at : File.mtime(__FILE__)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
data/test/benchmark/app.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
# https://github.com/rails-api/active_model_serializers/pull/872
|
2
|
-
# approx ref 792fb8a9053f8db3c562dae4f40907a582dd1720 to test against
|
3
|
-
require 'bundler/setup'
|
4
|
-
|
5
|
-
require 'rails'
|
6
|
-
require 'active_model'
|
7
|
-
require 'active_support'
|
8
|
-
require 'active_support/json'
|
9
|
-
require 'action_controller'
|
10
|
-
require 'action_controller/test_case'
|
11
|
-
require 'action_controller/railtie'
|
12
|
-
abort "Rails application already defined: #{Rails.application.class}" if Rails.application
|
13
|
-
|
14
|
-
class NullLogger < Logger
|
15
|
-
def initialize(*_args)
|
16
|
-
end
|
17
|
-
|
18
|
-
def add(*_args, &_block)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
class BenchmarkLogger < ActiveSupport::Logger
|
22
|
-
def initialize
|
23
|
-
@file = StringIO.new
|
24
|
-
super(@file)
|
25
|
-
end
|
26
|
-
|
27
|
-
def messages
|
28
|
-
@file.rewind
|
29
|
-
@file.read
|
30
|
-
end
|
31
|
-
end
|
32
|
-
# ref: https://gist.github.com/bf4/8744473
|
33
|
-
class BenchmarkApp < Rails::Application
|
34
|
-
# Set up production configuration
|
35
|
-
config.eager_load = true
|
36
|
-
config.cache_classes = true
|
37
|
-
# CONFIG: CACHE_ON={on,off}
|
38
|
-
config.action_controller.perform_caching = ENV['CACHE_ON'] != 'off'
|
39
|
-
config.action_controller.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
|
40
|
-
|
41
|
-
config.active_support.test_order = :random
|
42
|
-
config.secret_token = 'S' * 30
|
43
|
-
config.secret_key_base = 'abc123'
|
44
|
-
config.consider_all_requests_local = false
|
45
|
-
|
46
|
-
# otherwise deadlock occurred
|
47
|
-
config.middleware.delete 'Rack::Lock'
|
48
|
-
|
49
|
-
# to disable log files
|
50
|
-
config.logger = NullLogger.new
|
51
|
-
config.active_support.deprecation = :log
|
52
|
-
config.log_level = :info
|
53
|
-
end
|
54
|
-
|
55
|
-
require 'active_model_serializers'
|
56
|
-
|
57
|
-
# Initialize app before any serializers are defined, for running across revisions.
|
58
|
-
# ref: https://github.com/rails-api/active_model_serializers/pull/1478
|
59
|
-
Rails.application.initialize!
|
60
|
-
|
@@ -1,67 +0,0 @@
|
|
1
|
-
require 'benchmark/ips'
|
2
|
-
require 'json'
|
3
|
-
|
4
|
-
# Add benchmarking runner from ruby-bench-suite
|
5
|
-
# https://github.com/ruby-bench/ruby-bench-suite/blob/master/rails/benchmarks/support/benchmark_rails.rb
|
6
|
-
module Benchmark
|
7
|
-
module ActiveModelSerializers
|
8
|
-
module TestMethods
|
9
|
-
def request(method, path)
|
10
|
-
response = Rack::MockRequest.new(BenchmarkApp).send(method, path)
|
11
|
-
if response.status.in?([404, 500])
|
12
|
-
fail "omg, #{method}, #{path}, '#{response.status}', '#{response.body}'"
|
13
|
-
end
|
14
|
-
response
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
# extend Benchmark with an `ams` method
|
19
|
-
def ams(label = nil, time:, disable_gc: true, warmup: 3, &block)
|
20
|
-
fail ArgumentError.new, 'block should be passed' unless block_given?
|
21
|
-
|
22
|
-
if disable_gc
|
23
|
-
GC.disable
|
24
|
-
else
|
25
|
-
GC.enable
|
26
|
-
end
|
27
|
-
|
28
|
-
report = Benchmark.ips(time, warmup, true) do |x|
|
29
|
-
x.report(label) { yield }
|
30
|
-
end
|
31
|
-
|
32
|
-
entry = report.entries.first
|
33
|
-
|
34
|
-
output = {
|
35
|
-
label: label,
|
36
|
-
version: ::ActiveModel::Serializer::VERSION.to_s,
|
37
|
-
rails_version: ::Rails.version.to_s,
|
38
|
-
iterations_per_second: entry.ips,
|
39
|
-
iterations_per_second_standard_deviation: entry.error_percentage,
|
40
|
-
total_allocated_objects_per_iteration: count_total_allocated_objects(&block)
|
41
|
-
}.to_json
|
42
|
-
|
43
|
-
puts output
|
44
|
-
output
|
45
|
-
end
|
46
|
-
|
47
|
-
def count_total_allocated_objects
|
48
|
-
if block_given?
|
49
|
-
key =
|
50
|
-
if RUBY_VERSION < '2.2'
|
51
|
-
:total_allocated_object
|
52
|
-
else
|
53
|
-
:total_allocated_objects
|
54
|
-
end
|
55
|
-
|
56
|
-
before = GC.stat[key]
|
57
|
-
yield
|
58
|
-
after = GC.stat[key]
|
59
|
-
after - before
|
60
|
-
else
|
61
|
-
-1
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
extend Benchmark::ActiveModelSerializers
|
67
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
require_relative './benchmarking_support'
|
2
|
-
require_relative './app'
|
3
|
-
require_relative './setup'
|
4
|
-
|
5
|
-
time = 10
|
6
|
-
disable_gc = true
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
authors_query = Author.preload(:posts).preload(:profile)
|
11
|
-
author = authors_query.first
|
12
|
-
authors = authors_query.to_a
|
13
|
-
|
14
|
-
|
15
|
-
Benchmark.ams('Single: DefaultSerializer', time: time, disable_gc: disable_gc) do
|
16
|
-
ActiveModel::DefaultSerializer.new(author).to_json
|
17
|
-
end
|
18
|
-
|
19
|
-
Benchmark.ams('ArraySerializer', time: time, disable_gc: disable_gc) do
|
20
|
-
ActiveModel::ArraySerializer.new(authors).to_json
|
21
|
-
end
|
22
|
-
|
23
|
-
Benchmark.ams('ArraySerializer: each_serializer: DefaultSerializer', time: time, disable_gc: disable_gc) do
|
24
|
-
ActiveModel::ArraySerializer.new(authors, each_serializer:ActiveModel::DefaultSerializer).to_json
|
25
|
-
end
|
26
|
-
|
27
|
-
Benchmark.ams('FlatAuthorSerializer', time: time, disable_gc: disable_gc) do
|
28
|
-
FlatAuthorSerializer.new(author).to_json
|
29
|
-
end
|
30
|
-
|
31
|
-
Benchmark.ams('ArraySerializer: each_serializer: FlatAuthorSerializer', time: time, disable_gc: disable_gc) do
|
32
|
-
ActiveModel::ArraySerializer.new(authors, each_serializer: FlatAuthorSerializer).to_json
|
33
|
-
end
|
34
|
-
|
35
|
-
Benchmark.ams('AuthorWithDefaultRelationshipsSerializer', time: time, disable_gc: disable_gc) do
|
36
|
-
AuthorWithDefaultRelationshipsSerializer.new(author).to_json
|
37
|
-
end
|
38
|
-
|
39
|
-
Benchmark.ams('ArraySerializer: each_serializer: AuthorWithDefaultRelationshipsSerializer', time: time, disable_gc: disable_gc) do
|
40
|
-
ActiveModel::ArraySerializer.new(authors, each_serializer: AuthorWithDefaultRelationshipsSerializer).to_json
|
41
|
-
end
|
data/test/benchmark/setup.rb
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
###########################################
|
2
|
-
# Setup active record models
|
3
|
-
##########################################
|
4
|
-
require 'active_record'
|
5
|
-
require 'sqlite3'
|
6
|
-
|
7
|
-
|
8
|
-
# Change the following to reflect your database settings
|
9
|
-
ActiveRecord::Base.establish_connection(
|
10
|
-
adapter: 'sqlite3',
|
11
|
-
database: ':memory:'
|
12
|
-
)
|
13
|
-
|
14
|
-
# Don't show migration output when constructing fake db
|
15
|
-
ActiveRecord::Migration.verbose = false
|
16
|
-
|
17
|
-
ActiveRecord::Schema.define do
|
18
|
-
create_table :authors, force: true do |t|
|
19
|
-
t.string :name
|
20
|
-
end
|
21
|
-
|
22
|
-
create_table :posts, force: true do |t|
|
23
|
-
t.text :body
|
24
|
-
t.string :title
|
25
|
-
t.references :author
|
26
|
-
end
|
27
|
-
|
28
|
-
create_table :profiles, force: true do |t|
|
29
|
-
t.text :project_url
|
30
|
-
t.text :bio
|
31
|
-
t.date :birthday
|
32
|
-
t.references :author
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
class Author < ActiveRecord::Base
|
37
|
-
has_one :profile
|
38
|
-
has_many :posts
|
39
|
-
end
|
40
|
-
|
41
|
-
class Post < ActiveRecord::Base
|
42
|
-
belongs_to :author
|
43
|
-
end
|
44
|
-
|
45
|
-
class Profile < ActiveRecord::Base
|
46
|
-
belongs_to :author
|
47
|
-
end
|
48
|
-
|
49
|
-
# Build out the data to serialize
|
50
|
-
author = Author.create(name: 'Preston Sego')
|
51
|
-
Profile.create(project_url: 'https://github.com/NullVoxPopuli', author: author)
|
52
|
-
50.times do
|
53
|
-
Post.create(
|
54
|
-
body: 'something about how password restrictions are evil, and less secure, and with the math to prove it.',
|
55
|
-
title: 'Your bank is does not know how to do security',
|
56
|
-
author: author
|
57
|
-
)
|
58
|
-
end
|
59
|
-
|
60
|
-
ActiveModel::Serializer.root = false
|
61
|
-
ActiveModel::ArraySerializer.root = false
|
62
|
-
|
63
|
-
class FlatAuthorSerializer < ActiveModel::Serializer
|
64
|
-
attributes :id, :name
|
65
|
-
end
|
66
|
-
|
67
|
-
class AuthorWithDefaultRelationshipsSerializer < ActiveModel::Serializer
|
68
|
-
attributes :id, :name
|
69
|
-
|
70
|
-
has_one :profile
|
71
|
-
has_many :posts
|
72
|
-
end
|
73
|
-
|
74
|
-
# For debugging SQL output
|
75
|
-
#ActiveRecord::Base.logger = Logger.new(STDERR)
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -1,96 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
|
3
|
-
ActiveRecord::Base.establish_connection(
|
4
|
-
:adapter => 'sqlite3',
|
5
|
-
:database => ':memory:'
|
6
|
-
)
|
7
|
-
|
8
|
-
ActiveRecord::Schema.define do
|
9
|
-
create_table :ar_posts, force: true do |t|
|
10
|
-
t.string :title
|
11
|
-
t.text :body
|
12
|
-
t.belongs_to :ar_section, index: true
|
13
|
-
t.timestamps
|
14
|
-
end
|
15
|
-
|
16
|
-
create_table :ar_comments, force: true do |t|
|
17
|
-
t.text :body
|
18
|
-
t.belongs_to :ar_post, index: true
|
19
|
-
t.timestamps
|
20
|
-
end
|
21
|
-
|
22
|
-
create_table :ar_tags, force: true do |t|
|
23
|
-
t.string :name
|
24
|
-
end
|
25
|
-
|
26
|
-
create_table :ar_sections, force: true do |t|
|
27
|
-
t.string :name
|
28
|
-
end
|
29
|
-
|
30
|
-
create_table :ar_posts_tags, force: true do |t|
|
31
|
-
t.references :ar_post, :ar_tag, index: true
|
32
|
-
end
|
33
|
-
|
34
|
-
create_table :ar_comments_tags, force: true do |t|
|
35
|
-
t.references :ar_comment, :ar_tag, index: true
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
class ARPost < ActiveRecord::Base
|
40
|
-
has_many :ar_comments, class_name: 'ARComment'
|
41
|
-
has_and_belongs_to_many :ar_tags, class_name: 'ARTag', join_table: :ar_posts_tags
|
42
|
-
belongs_to :ar_section, class_name: 'ARSection'
|
43
|
-
end
|
44
|
-
|
45
|
-
class ARComment < ActiveRecord::Base
|
46
|
-
belongs_to :ar_post, class_name: 'ARPost'
|
47
|
-
has_and_belongs_to_many :ar_tags, class_name: 'ARTag', join_table: :ar_comments_tags
|
48
|
-
end
|
49
|
-
|
50
|
-
class ARTag < ActiveRecord::Base
|
51
|
-
end
|
52
|
-
|
53
|
-
class ARSection < ActiveRecord::Base
|
54
|
-
end
|
55
|
-
|
56
|
-
class ARPostSerializer < ActiveModel::Serializer
|
57
|
-
attributes :title, :body
|
58
|
-
|
59
|
-
has_many :ar_comments, :ar_tags
|
60
|
-
has_one :ar_section
|
61
|
-
end
|
62
|
-
|
63
|
-
class ARCommentSerializer < ActiveModel::Serializer
|
64
|
-
attributes :body
|
65
|
-
|
66
|
-
has_many :ar_tags
|
67
|
-
end
|
68
|
-
|
69
|
-
class ARTagSerializer < ActiveModel::Serializer
|
70
|
-
attributes :name
|
71
|
-
end
|
72
|
-
|
73
|
-
class ARSectionSerializer < ActiveModel::Serializer
|
74
|
-
attributes 'name'
|
75
|
-
end
|
76
|
-
|
77
|
-
class AREmbeddedSerializer < ActiveModel::Serializer
|
78
|
-
has_many :ar_tags, :ar_comments
|
79
|
-
end
|
80
|
-
|
81
|
-
ARPost.create(title: 'New post',
|
82
|
-
body: 'A body!!!',
|
83
|
-
ar_section: ARSection.create(name: 'ruby')).tap do |post|
|
84
|
-
|
85
|
-
short_tag = post.ar_tags.create(name: 'short')
|
86
|
-
whiny_tag = post.ar_tags.create(name: 'whiny')
|
87
|
-
happy_tag = ARTag.create(name: 'happy')
|
88
|
-
|
89
|
-
post.ar_comments.create(body: 'what a dumb post').tap do |comment|
|
90
|
-
comment.ar_tags.concat happy_tag, whiny_tag
|
91
|
-
end
|
92
|
-
|
93
|
-
post.ar_comments.create(body: 'i liked it').tap do |comment|
|
94
|
-
comment.ar_tags.concat happy_tag, short_tag
|
95
|
-
end
|
96
|
-
end
|