praxis 2.0.pre.1 → 2.0.pre.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +0 -1
- data/.ruby-version +1 -0
- data/.travis.yml +5 -20
- data/CHANGELOG.md +32 -0
- data/Gemfile +1 -1
- data/Guardfile +2 -1
- data/Rakefile +1 -7
- data/TODO.md +28 -0
- data/lib/api_browser/package-lock.json +7110 -0
- data/lib/praxis.rb +7 -4
- data/lib/praxis/action_definition.rb +10 -17
- data/lib/praxis/action_definition/headers_dsl_compiler.rb +1 -1
- data/lib/praxis/api_general_info.rb +21 -0
- data/lib/praxis/application.rb +2 -3
- data/lib/praxis/bootloader_stages/routing.rb +2 -4
- data/lib/praxis/config.rb +1 -1
- data/lib/praxis/docs/generator.rb +11 -6
- data/lib/praxis/docs/open_api_generator.rb +255 -0
- data/lib/praxis/docs/openapi/info_object.rb +31 -0
- data/lib/praxis/docs/openapi/media_type_object.rb +59 -0
- data/lib/praxis/docs/openapi/operation_object.rb +40 -0
- data/lib/praxis/docs/openapi/parameter_object.rb +69 -0
- data/lib/praxis/docs/openapi/paths_object.rb +58 -0
- data/lib/praxis/docs/openapi/request_body_object.rb +51 -0
- data/lib/praxis/docs/openapi/response_object.rb +63 -0
- data/lib/praxis/docs/openapi/responses_object.rb +44 -0
- data/lib/praxis/docs/openapi/schema_object.rb +87 -0
- data/lib/praxis/docs/openapi/server_object.rb +24 -0
- data/lib/praxis/docs/openapi/tag_object.rb +21 -0
- data/lib/praxis/extensions/attribute_filtering.rb +2 -0
- data/lib/praxis/extensions/attribute_filtering/active_record_filter_query_builder.rb +148 -157
- data/lib/praxis/extensions/attribute_filtering/active_record_patches.rb +15 -0
- data/lib/praxis/extensions/attribute_filtering/active_record_patches/5x.rb +90 -0
- data/lib/praxis/extensions/attribute_filtering/active_record_patches/6_0.rb +68 -0
- data/lib/praxis/extensions/attribute_filtering/active_record_patches/6_1_plus.rb +58 -0
- data/lib/praxis/extensions/attribute_filtering/filter_tree_node.rb +35 -0
- data/lib/praxis/extensions/attribute_filtering/filtering_params.rb +13 -12
- data/lib/praxis/extensions/attribute_filtering/sequel_filter_query_builder.rb +3 -2
- data/lib/praxis/extensions/field_selection/active_record_query_selector.rb +24 -30
- data/lib/praxis/extensions/field_selection/field_selector.rb +4 -0
- data/lib/praxis/extensions/field_selection/sequel_query_selector.rb +32 -39
- data/lib/praxis/extensions/pagination.rb +130 -0
- data/lib/praxis/extensions/pagination/active_record_pagination_handler.rb +42 -0
- data/lib/praxis/extensions/pagination/header_generator.rb +70 -0
- data/lib/praxis/extensions/pagination/ordering_params.rb +234 -0
- data/lib/praxis/extensions/pagination/pagination_handler.rb +68 -0
- data/lib/praxis/extensions/pagination/pagination_params.rb +374 -0
- data/lib/praxis/extensions/pagination/sequel_pagination_handler.rb +45 -0
- data/lib/praxis/handlers/json.rb +2 -0
- data/lib/praxis/handlers/www_form.rb +5 -0
- data/lib/praxis/handlers/{xml.rb → xml-sample.rb} +6 -0
- data/lib/praxis/links.rb +4 -0
- data/lib/praxis/mapper/active_model_compat.rb +57 -4
- data/lib/praxis/mapper/resource.rb +18 -11
- data/lib/praxis/mapper/selector_generator.rb +99 -75
- data/lib/praxis/mapper/sequel_compat.rb +43 -3
- data/lib/praxis/media_type.rb +1 -56
- data/lib/praxis/media_type_identifier.rb +2 -1
- data/lib/praxis/multipart/part.rb +5 -2
- data/lib/praxis/notifications.rb +1 -1
- data/lib/praxis/plugins/mapper_plugin.rb +17 -3
- data/lib/praxis/plugins/pagination_plugin.rb +71 -0
- data/lib/praxis/resource_definition.rb +8 -16
- data/lib/praxis/response_definition.rb +1 -1
- data/lib/praxis/route.rb +3 -5
- data/lib/praxis/routing_config.rb +3 -7
- data/lib/praxis/tasks/api_docs.rb +23 -0
- data/lib/praxis/tasks/routes.rb +9 -14
- data/lib/praxis/trait.rb +1 -1
- data/lib/praxis/types/media_type_common.rb +12 -2
- data/lib/praxis/types/multipart.rb +1 -1
- data/lib/praxis/types/multipart_array.rb +64 -2
- data/lib/praxis/types/multipart_array/part_definition.rb +1 -1
- data/lib/praxis/validation_handler.rb +1 -2
- data/lib/praxis/version.rb +1 -1
- data/praxis.gemspec +11 -9
- data/spec/functional_spec.rb +9 -6
- data/spec/praxis/action_definition_spec.rb +4 -16
- data/spec/praxis/api_general_info_spec.rb +6 -6
- data/spec/praxis/application_spec.rb +1 -1
- data/spec/praxis/collection_spec.rb +3 -2
- data/spec/praxis/config_spec.rb +2 -2
- data/spec/praxis/extensions/attribute_filtering/active_record_filter_query_builder_spec.rb +304 -0
- data/spec/praxis/extensions/attribute_filtering/filter_tree_node_spec.rb +39 -0
- data/spec/praxis/extensions/attribute_filtering/filtering_params_spec.rb +34 -0
- data/spec/praxis/extensions/field_expansion_spec.rb +6 -24
- data/spec/praxis/extensions/field_selection/active_record_query_selector_spec.rb +110 -0
- data/spec/praxis/extensions/field_selection/sequel_query_selector_spec.rb +148 -0
- data/spec/praxis/extensions/pagination/active_record_pagination_handler_spec.rb +130 -0
- data/spec/praxis/extensions/support/spec_resources_active_model.rb +173 -0
- data/spec/praxis/extensions/support/spec_resources_sequel.rb +106 -0
- data/spec/praxis/mapper/selector_generator_spec.rb +306 -282
- data/spec/praxis/media_type_spec.rb +5 -139
- data/spec/praxis/middleware_app_spec.rb +1 -1
- data/spec/praxis/request_spec.rb +3 -22
- data/spec/praxis/request_stages/action_spec.rb +8 -1
- data/spec/praxis/resource_definition_spec.rb +1 -1
- data/spec/praxis/response_definition_spec.rb +15 -13
- data/spec/praxis/response_spec.rb +1 -1
- data/spec/praxis/route_spec.rb +2 -9
- data/spec/praxis/router_spec.rb +1 -1
- data/spec/praxis/routing_config_spec.rb +4 -13
- data/spec/praxis/types/multipart_array_spec.rb +4 -21
- data/spec/spec_app/app/controllers/instances.rb +1 -1
- data/spec/spec_app/config/environment.rb +0 -2
- data/spec/spec_app/design/api.rb +7 -1
- data/spec/spec_app/design/media_types/instance.rb +0 -8
- data/spec/spec_app/design/media_types/volume.rb +0 -12
- data/spec/spec_app/design/resources/instances.rb +1 -2
- data/spec/spec_helper.rb +17 -0
- data/spec/support/be_deep_equal_matcher.rb +39 -0
- data/spec/support/spec_media_types.rb +0 -73
- data/spec/support/spec_resources.rb +42 -49
- metadata +91 -56
- data/spec/praxis/handlers/xml_spec.rb +0 -177
- data/spec/praxis/links_spec.rb +0 -68
- data/spec/spec_app/app/models/person.rb +0 -3
@@ -0,0 +1,173 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
require 'praxis/mapper/active_model_compat'
|
4
|
+
|
5
|
+
# Creates a new in-memory DB, and the necessary tables (and mini-seeds) for the models in this file
|
6
|
+
def create_tables
|
7
|
+
|
8
|
+
ActiveRecord::Base.establish_connection(
|
9
|
+
adapter: 'sqlite3',
|
10
|
+
dbfile: ':memory:',
|
11
|
+
database: ':memory:'
|
12
|
+
)
|
13
|
+
|
14
|
+
ActiveRecord::Schema.define do
|
15
|
+
ActiveRecord::Migration.suppress_messages do
|
16
|
+
create_table :active_books do |table|
|
17
|
+
table.column :simple_name, :string
|
18
|
+
table.column :added_column, :string
|
19
|
+
table.column :category_uuid, :string
|
20
|
+
table.column :author_id, :integer
|
21
|
+
end
|
22
|
+
|
23
|
+
create_table :active_authors do |table|
|
24
|
+
table.column :name, :string
|
25
|
+
end
|
26
|
+
|
27
|
+
create_table :active_categories do |table|
|
28
|
+
table.column :uuid, :string
|
29
|
+
table.column :name, :string
|
30
|
+
end
|
31
|
+
|
32
|
+
create_table :active_tags do |table|
|
33
|
+
table.column :name, :string
|
34
|
+
end
|
35
|
+
|
36
|
+
create_table :active_taggings do |table|
|
37
|
+
table.column :book_id, :integer
|
38
|
+
table.column :tag_id, :integer
|
39
|
+
table.column :label, :string, null: true
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
create_tables
|
46
|
+
|
47
|
+
class ActiveBook < ActiveRecord::Base
|
48
|
+
include Praxis::Mapper::ActiveModelCompat
|
49
|
+
|
50
|
+
belongs_to :category, class_name: 'ActiveCategory', foreign_key: :category_uuid, primary_key: :uuid
|
51
|
+
belongs_to :author, class_name: 'ActiveAuthor'
|
52
|
+
|
53
|
+
has_many :taggings, class_name: 'ActiveTagging', foreign_key: :book_id
|
54
|
+
has_many :primary_taggings, lambda { where(label: 'primary')}, class_name: 'ActiveTagging', foreign_key: :book_id
|
55
|
+
|
56
|
+
has_many :tags, class_name: 'ActiveTag', through: :taggings
|
57
|
+
has_many :primary_tags, class_name: 'ActiveTag', through: :primary_taggings, source: :tag
|
58
|
+
end
|
59
|
+
|
60
|
+
class ActiveAuthor < ActiveRecord::Base
|
61
|
+
include Praxis::Mapper::ActiveModelCompat
|
62
|
+
has_many :books, class_name: 'ActiveBook', foreign_key: :author_id
|
63
|
+
end
|
64
|
+
|
65
|
+
class ActiveCategory < ActiveRecord::Base
|
66
|
+
include Praxis::Mapper::ActiveModelCompat
|
67
|
+
has_many :books, class_name: 'ActiveBook', primary_key: :uuid, foreign_key: :category_uuid
|
68
|
+
end
|
69
|
+
|
70
|
+
class ActiveTag < ActiveRecord::Base
|
71
|
+
include Praxis::Mapper::ActiveModelCompat
|
72
|
+
has_many :taggings, class_name: 'ActiveTagging', foreign_key: :tag_id
|
73
|
+
has_many :books, class_name: 'ActiveBook', through: :taggings, source: :book
|
74
|
+
end
|
75
|
+
|
76
|
+
class ActiveTagging < ActiveRecord::Base
|
77
|
+
include Praxis::Mapper::ActiveModelCompat
|
78
|
+
belongs_to :book, class_name: 'ActiveBook', foreign_key: :book_id
|
79
|
+
belongs_to :tag, class_name: 'ActiveTag', foreign_key: :tag_id
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
# A set of resource classes for use in specs
|
84
|
+
class ActiveBaseResource < Praxis::Mapper::Resource
|
85
|
+
end
|
86
|
+
|
87
|
+
class ActiveAuthorResource < ActiveBaseResource
|
88
|
+
model ActiveAuthor
|
89
|
+
|
90
|
+
property :display_name, dependencies: [:name]
|
91
|
+
end
|
92
|
+
|
93
|
+
class ActiveCategoryResource < ActiveBaseResource
|
94
|
+
model ActiveCategory
|
95
|
+
end
|
96
|
+
|
97
|
+
class ActiveTagResource < ActiveBaseResource
|
98
|
+
model ActiveTag
|
99
|
+
end
|
100
|
+
|
101
|
+
class ActiveBookResource < ActiveBaseResource
|
102
|
+
model ActiveBook
|
103
|
+
|
104
|
+
filters_mapping(
|
105
|
+
id: :id,
|
106
|
+
category_uuid: :category_uuid,
|
107
|
+
'fake_nested.name': 'simple_name',
|
108
|
+
'name': 'simple_name',
|
109
|
+
'name_is_not': lambda do |spec| # Silly way to use a proc, but good enough for testing
|
110
|
+
{ name: :simple_name, value: spec[:value] , op: '!=' } # Can be an array for multiple conditions as well
|
111
|
+
end,
|
112
|
+
'author.id': 'author.id',
|
113
|
+
'author.name': 'author.name',
|
114
|
+
'taggings.label': 'taggings.label',
|
115
|
+
'taggings.tag_id': 'taggings.tag_id',
|
116
|
+
'taggings.tag.taggings.tag_id': 'taggings.tag.taggings.tag_id',
|
117
|
+
'tags.name': 'tags.name',
|
118
|
+
'primary_tags.name': 'primary_tags.name',
|
119
|
+
'category.name': 'category.name',
|
120
|
+
'category.books.name': 'category.books.simple_name',
|
121
|
+
'category.books.taggings.tag_id': 'category.books.taggings.tag_id',
|
122
|
+
'category.books.taggings.label': 'category.books.taggings.label',
|
123
|
+
)
|
124
|
+
# Forces to add an extra column (added_column)...and yet another (author_id) that will serve
|
125
|
+
# to check that if that's already automatically added due to an association, it won't interfere or duplicate
|
126
|
+
property :author, dependencies: [:author, :added_column, :author_id]
|
127
|
+
|
128
|
+
property :name, dependencies: [:simple_name]
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
def seed_data
|
133
|
+
cat1 = ActiveCategory.create( id: 1 , uuid: 'deadbeef1', name: 'cat1' )
|
134
|
+
cat2 = ActiveCategory.create( id: 2 , uuid: 'deadbeef2', name: 'cat2' )
|
135
|
+
|
136
|
+
author1 = ActiveAuthor.create( id: 11, name: 'author1' )
|
137
|
+
author2 = ActiveAuthor.create( id: 22, name: 'author2' )
|
138
|
+
author3 = ActiveAuthor.create( id: 33, name: nil )
|
139
|
+
|
140
|
+
tag_blue = ActiveTag.create(id: 1 , name: 'blue' )
|
141
|
+
tag_red = ActiveTag.create(id: 2 , name: 'red' )
|
142
|
+
tag_green = ActiveTag.create(id: 3 , name: 'green' )
|
143
|
+
|
144
|
+
book1 = ActiveBook.create( id: 1 , simple_name: 'Book1', category_uuid: 'deadbeef1')
|
145
|
+
book1.author = author1
|
146
|
+
book1.category = cat1
|
147
|
+
book1.save
|
148
|
+
ActiveTagging.create(book: book1, tag: tag_blue, label: 'primary')
|
149
|
+
ActiveTagging.create(book: book1, tag: tag_red)
|
150
|
+
ActiveTagging.create(book: book1, tag: tag_green, label: 'primary')
|
151
|
+
|
152
|
+
|
153
|
+
book2 = ActiveBook.create( id: 2 , simple_name: 'Book2', category_uuid: 'deadbeef1')
|
154
|
+
book2.author = author2
|
155
|
+
book2.category = cat2
|
156
|
+
book2.save
|
157
|
+
ActiveTagging.create(book: book2, tag: tag_red, label: 'primary')
|
158
|
+
|
159
|
+
book3 = ActiveBook.create( id: 3 , simple_name: 'Book3', category_uuid: 'deadbeef1')
|
160
|
+
book3.author = author3
|
161
|
+
book3.save
|
162
|
+
ActiveTagging.create(book: book3, tag: tag_red, label: 'primary')
|
163
|
+
|
164
|
+
# More stuff
|
165
|
+
|
166
|
+
10.times do |i|
|
167
|
+
bid = 1000+i
|
168
|
+
ActiveBook.create( id: bid , simple_name: "Book#{bid}")
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
seed_data
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
|
3
|
+
require 'praxis/mapper/sequel_compat'
|
4
|
+
|
5
|
+
|
6
|
+
# Creates a new in-memory DB, and the necessary tables (and mini-seeds) for the sequel models in this file
|
7
|
+
def create_and_seed_tables
|
8
|
+
sequeldb = Sequel.sqlite
|
9
|
+
# sequeldb.loggers = [Logger.new($stdout)] # Uncomment to see sequel logs
|
10
|
+
|
11
|
+
sequeldb.create_table! :sequel_simple_models do
|
12
|
+
primary_key :id
|
13
|
+
String :simple_name
|
14
|
+
Integer :parent_id
|
15
|
+
String :parent_uuid
|
16
|
+
Integer :other_model_id
|
17
|
+
String :added_column
|
18
|
+
end
|
19
|
+
sequeldb.create_table! :sequel_other_models do
|
20
|
+
primary_key :id
|
21
|
+
end
|
22
|
+
sequeldb.create_table! :sequel_parent_models do
|
23
|
+
primary_key :id
|
24
|
+
String :uuid
|
25
|
+
end
|
26
|
+
sequeldb.create_table! :sequel_tag_models do
|
27
|
+
primary_key :id
|
28
|
+
String :tag_name
|
29
|
+
end
|
30
|
+
sequeldb.create_table! :sequel_simple_models_sequel_tag_models do
|
31
|
+
Integer :sequel_simple_model_id
|
32
|
+
Integer :tag_id
|
33
|
+
end
|
34
|
+
|
35
|
+
sequeldb[:sequel_parent_models] << { id: 1 , uuid: 'deadbeef1'}
|
36
|
+
sequeldb[:sequel_parent_models] << { id: 2 , uuid: 'deadbeef2'}
|
37
|
+
|
38
|
+
sequeldb[:sequel_other_models] << { id: 11 }
|
39
|
+
sequeldb[:sequel_other_models] << { id: 22 }
|
40
|
+
|
41
|
+
sequeldb[:sequel_tag_models] << { id: 1 , tag_name: 'blue' }
|
42
|
+
sequeldb[:sequel_tag_models] << { id: 2 , tag_name: 'red' }
|
43
|
+
|
44
|
+
# Simple model 1 is tagged as blue and red
|
45
|
+
sequeldb[:sequel_simple_models_sequel_tag_models] << { sequel_simple_model_id: 1, tag_id: 1 }
|
46
|
+
sequeldb[:sequel_simple_models_sequel_tag_models] << { sequel_simple_model_id: 1, tag_id: 2 }
|
47
|
+
# Simple model 2 is tagged as red
|
48
|
+
sequeldb[:sequel_simple_models_sequel_tag_models] << { sequel_simple_model_id: 2, tag_id: 2 }
|
49
|
+
|
50
|
+
# It's weird to have a parent id and parent uuid (which points to different actual parents)
|
51
|
+
# But it allows us to check pointing to both PKs and not PK columns
|
52
|
+
sequeldb[:sequel_simple_models] << { id: 1 , simple_name: 'Simple1', parent_id: 1, other_model_id: 11, parent_uuid: 'deadbeef1'}
|
53
|
+
sequeldb[:sequel_simple_models] << { id: 2 , simple_name: 'Simple2', parent_id: 2, other_model_id: 22, parent_uuid: 'deadbeef1'}
|
54
|
+
end
|
55
|
+
|
56
|
+
create_and_seed_tables
|
57
|
+
|
58
|
+
class SequelSimpleModel < Sequel::Model
|
59
|
+
include Praxis::Mapper::SequelCompat
|
60
|
+
|
61
|
+
many_to_one :parent, class: 'SequelParentModel'
|
62
|
+
many_to_one :other_model, class: 'SequelOtherModel'
|
63
|
+
many_to_many :tags, class: 'SequelTagModel'
|
64
|
+
end
|
65
|
+
|
66
|
+
class SequelOtherModel < Sequel::Model
|
67
|
+
include Praxis::Mapper::SequelCompat
|
68
|
+
end
|
69
|
+
|
70
|
+
class SequelParentModel < Sequel::Model
|
71
|
+
include Praxis::Mapper::SequelCompat
|
72
|
+
one_to_many :children, class: 'SequelSimpleModel', primary_key: :uuid, key: :parent_uuid
|
73
|
+
end
|
74
|
+
|
75
|
+
class SequelTagModel < Sequel::Model
|
76
|
+
include Praxis::Mapper::SequelCompat
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
# A set of resource classes for use in specs
|
81
|
+
class SequelBaseResource < Praxis::Mapper::Resource
|
82
|
+
end
|
83
|
+
|
84
|
+
class SequelOtherResource < SequelBaseResource
|
85
|
+
model SequelOtherModel
|
86
|
+
|
87
|
+
property :display_name, dependencies: [:name]
|
88
|
+
end
|
89
|
+
|
90
|
+
class SequelParentResource < SequelBaseResource
|
91
|
+
model SequelParentModel
|
92
|
+
end
|
93
|
+
|
94
|
+
class SequelTagResource < SequelBaseResource
|
95
|
+
model SequelTagModel
|
96
|
+
end
|
97
|
+
|
98
|
+
class SequelSimpleResource < SequelBaseResource
|
99
|
+
model SequelSimpleModel
|
100
|
+
|
101
|
+
# Forces to add an extra column (added_column)...and yet another (parent_id) that will serve
|
102
|
+
# to check that if that's already automatically added due to an association, it won't interfere or duplicate
|
103
|
+
property :parent, dependencies: [:parent, :added_column, :parent_id]
|
104
|
+
|
105
|
+
property :name, dependencies: [:simple_name]
|
106
|
+
end
|
@@ -1,301 +1,325 @@
|
|
1
|
-
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
# describe Praxis::Mapper::SelectorGenerator do
|
4
|
-
# let(:properties) { {} }
|
5
|
-
# let(:resource) { BlogResource }
|
6
|
-
# subject(:generator) {Praxis::Mapper::SelectorGenerator.new }
|
7
3
|
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
describe Praxis::Mapper::SelectorGenerator do
|
5
|
+
let(:resource) { SimpleResource }
|
6
|
+
subject(:generator) {described_class.new }
|
11
7
|
|
12
|
-
#
|
8
|
+
context '#add' do
|
9
|
+
let(:resource) { SimpleResource }
|
10
|
+
shared_examples 'a proper selector' do
|
11
|
+
it { expect(generator.add(resource, fields).dump).to be_deep_equal selectors }
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'basic combos' do
|
15
|
+
context 'direct column fields' do
|
16
|
+
let(:fields) { {id: true, foobar: true} }
|
17
|
+
let(:selectors) do
|
18
|
+
{
|
19
|
+
model: SimpleModel,
|
20
|
+
columns: [:id, :foobar]
|
21
|
+
}
|
22
|
+
end
|
23
|
+
it_behaves_like 'a proper selector'
|
24
|
+
end
|
13
25
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
26
|
+
context 'aliased column fields' do
|
27
|
+
let(:fields) { {id: true, name: true} }
|
28
|
+
let(:selectors) do
|
29
|
+
{
|
30
|
+
model: SimpleModel,
|
31
|
+
columns: [:id, :simple_name]
|
32
|
+
}
|
33
|
+
end
|
34
|
+
it_behaves_like 'a proper selector'
|
35
|
+
end
|
24
36
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
37
|
+
context 'pure associations without recursion' do
|
38
|
+
let(:fields) { {other_model: true} }
|
39
|
+
let(:selectors) do
|
40
|
+
{
|
41
|
+
model: SimpleModel,
|
42
|
+
columns: [:other_model_id], # FK of the other_model association
|
43
|
+
tracks: {
|
44
|
+
other_model: {
|
45
|
+
columns: [:id], # joining key for the association
|
46
|
+
model: OtherModel
|
47
|
+
}
|
48
|
+
}
|
49
|
+
}
|
50
|
+
end
|
51
|
+
it_behaves_like 'a proper selector'
|
52
|
+
end
|
29
53
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
#
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
54
|
+
context 'aliased associations without recursion' do
|
55
|
+
let(:fields) { {other_resource: true} }
|
56
|
+
let(:selectors) do
|
57
|
+
{
|
58
|
+
model: SimpleModel,
|
59
|
+
columns: [:other_model_id], # FK of the other_model association
|
60
|
+
tracks: {
|
61
|
+
other_model: {
|
62
|
+
columns: [:id], # joining key for the association
|
63
|
+
model: OtherModel
|
64
|
+
}
|
65
|
+
}
|
66
|
+
}
|
67
|
+
end
|
68
|
+
it_behaves_like 'a proper selector'
|
69
|
+
end
|
70
|
+
context 'aliased associations without recursion (that map to columns and other associations)' do
|
71
|
+
let(:fields) { {aliased_method: true} }
|
72
|
+
let(:selectors) do
|
73
|
+
{
|
74
|
+
model: SimpleModel,
|
75
|
+
columns: [:column1, :other_model_id], # other_model_id => because of the association
|
76
|
+
tracks: {
|
77
|
+
other_model: {
|
78
|
+
columns: [:id], # joining key for the association
|
79
|
+
model: OtherModel
|
80
|
+
}
|
81
|
+
}
|
82
|
+
}
|
83
|
+
end
|
84
|
+
it_behaves_like 'a proper selector'
|
85
|
+
end
|
44
86
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
87
|
+
context 'redefined associations that add some extra columns (would need both the underlying association AND the columns in place)' do
|
88
|
+
let(:fields) { {parent: true} }
|
89
|
+
let(:selectors) do
|
90
|
+
{
|
91
|
+
model: SimpleModel,
|
92
|
+
columns: [:parent_id, :added_column],
|
93
|
+
tracks: {
|
94
|
+
parent: {
|
95
|
+
columns: [:id],
|
96
|
+
model: ParentModel
|
97
|
+
}
|
98
|
+
}
|
99
|
+
}
|
100
|
+
end
|
101
|
+
it_behaves_like 'a proper selector'
|
102
|
+
end
|
58
103
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
# select: Set.new([]),
|
70
|
-
# track: Set.new([:comments])
|
71
|
-
# }
|
72
|
-
# }
|
73
|
-
# end
|
74
|
-
# it 'generates the correct set of selectors' do
|
75
|
-
# generator.selectors.should eq expected_selectors
|
76
|
-
# end
|
77
|
-
# end
|
104
|
+
context 'a simple property that requires all fields' do
|
105
|
+
let(:fields) { {everything: true} }
|
106
|
+
let(:selectors) do
|
107
|
+
{
|
108
|
+
model: SimpleModel,
|
109
|
+
columns: [:*],
|
110
|
+
}
|
111
|
+
end
|
112
|
+
it_behaves_like 'a proper selector'
|
113
|
+
end
|
78
114
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
#
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
# select: Set.new([]),
|
90
|
-
# track: Set.new([:other_commented_posts])
|
91
|
-
# }
|
92
|
-
# }
|
93
|
-
# end
|
94
|
-
# it 'generates the correct set of selectors' do
|
95
|
-
# generator.selectors.should eq expected_selectors
|
96
|
-
# end
|
97
|
-
# end
|
115
|
+
context 'a simple property that requires itself' do
|
116
|
+
let(:fields) { {circular_dep: true} }
|
117
|
+
let(:selectors) do
|
118
|
+
{
|
119
|
+
model: SimpleModel,
|
120
|
+
columns: [:circular_dep, :column1], #allows to "expand" the dependency into itself + others
|
121
|
+
}
|
122
|
+
end
|
123
|
+
it_behaves_like 'a proper selector'
|
124
|
+
end
|
98
125
|
|
126
|
+
context 'a simple property without dependencies' do
|
127
|
+
let(:fields) { {no_deps: true} }
|
128
|
+
let(:selectors) do
|
129
|
+
{
|
130
|
+
model: SimpleModel
|
131
|
+
}
|
132
|
+
end
|
133
|
+
it_behaves_like 'a proper selector'
|
134
|
+
end
|
99
135
|
|
100
|
-
|
101
|
-
# let(:properties) { {composite_model: {id: true, type: true} } }
|
102
|
-
# let(:resource) { OtherResource }
|
103
|
-
# let(:expected_selectors) do
|
104
|
-
# {
|
105
|
-
# OtherModel => {
|
106
|
-
# select: Set.new([:composite_id,:composite_type]),
|
107
|
-
# track: Set.new([:composite_model])
|
108
|
-
# },
|
109
|
-
# CompositeIdModel => {
|
110
|
-
# select: Set.new([:id,:type]),
|
111
|
-
# track: Set.new
|
112
|
-
# }
|
113
|
-
# }
|
114
|
-
# end
|
115
|
-
# it 'generates the correct set of selectors' do
|
116
|
-
# generator.selectors.should eq expected_selectors
|
117
|
-
# end
|
118
|
-
# end
|
119
|
-
# end
|
136
|
+
end
|
120
137
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
138
|
+
context 'nested tracking' do
|
139
|
+
context 'pure associations follow the nested fields' do
|
140
|
+
let(:fields) do
|
141
|
+
{
|
142
|
+
other_model: {
|
143
|
+
id: true
|
144
|
+
}
|
145
|
+
}
|
146
|
+
end
|
147
|
+
let(:selectors) do
|
148
|
+
{
|
149
|
+
model: SimpleModel,
|
150
|
+
columns: [:other_model_id],
|
151
|
+
tracks: {
|
152
|
+
other_model: {
|
153
|
+
model: OtherModel,
|
154
|
+
columns: [:id]
|
155
|
+
}
|
156
|
+
}
|
157
|
+
}
|
158
|
+
end
|
159
|
+
it_behaves_like 'a proper selector'
|
160
|
+
end
|
135
161
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
162
|
+
context 'Aliased resources disregard any nested fields...' do
|
163
|
+
let(:fields) do
|
164
|
+
{
|
165
|
+
other_resource: {
|
166
|
+
id: true
|
167
|
+
}
|
168
|
+
}
|
169
|
+
end
|
170
|
+
let(:selectors) do
|
171
|
+
{
|
172
|
+
model: SimpleModel,
|
173
|
+
columns: [:other_model_id],
|
174
|
+
tracks: {
|
175
|
+
other_model: {
|
176
|
+
model: OtherModel,
|
177
|
+
columns: [:id]
|
178
|
+
}
|
179
|
+
}
|
180
|
+
}
|
181
|
+
end
|
182
|
+
it_behaves_like 'a proper selector'
|
183
|
+
end
|
140
184
|
|
141
|
-
|
142
|
-
|
143
|
-
#
|
144
|
-
#
|
145
|
-
#
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
185
|
+
context 'merging multiple tracks with the same name within a node' do
|
186
|
+
let(:fields) do
|
187
|
+
{ # Both everything_from_parent and parent will track the underlying 'parent' assoc
|
188
|
+
# ...and the final respective fields and tracks will need to be merged together.
|
189
|
+
# columns will be merged by just *, and tracks will merge true with simple children
|
190
|
+
everything_from_parent: true,
|
191
|
+
parent: {
|
192
|
+
simple_children: true
|
193
|
+
}
|
194
|
+
}
|
195
|
+
end
|
196
|
+
let(:selectors) do
|
197
|
+
{
|
198
|
+
model: SimpleModel,
|
199
|
+
columns: [:parent_id, :added_column],
|
200
|
+
tracks: {
|
201
|
+
parent: {
|
202
|
+
model: ParentModel,
|
203
|
+
columns: [:*],
|
204
|
+
tracks: {
|
205
|
+
simple_children: {
|
206
|
+
model: SimpleModel,
|
207
|
+
columns: [:parent_id]
|
208
|
+
}
|
209
|
+
}
|
210
|
+
}
|
211
|
+
}
|
212
|
+
}
|
213
|
+
end
|
214
|
+
it_behaves_like 'a proper selector'
|
215
|
+
end
|
216
|
+
end
|
155
217
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
# end
|
218
|
+
context 'string associations' do
|
219
|
+
context 'that specify a direct existing colum in the target dependency' do
|
220
|
+
let(:fields) { { direct_other_name: true } }
|
221
|
+
let(:selectors) do
|
222
|
+
{
|
223
|
+
model: SimpleModel,
|
224
|
+
columns: [:other_model_id],
|
225
|
+
tracks: {
|
226
|
+
other_model: {
|
227
|
+
model: OtherModel,
|
228
|
+
columns: [:id, :name]
|
229
|
+
}
|
230
|
+
}
|
231
|
+
}
|
232
|
+
end
|
233
|
+
it_behaves_like 'a proper selector'
|
234
|
+
end
|
174
235
|
|
236
|
+
context 'that specify an aliased property in the target dependency' do
|
237
|
+
let(:fields) { { aliased_other_name: true } }
|
238
|
+
let(:selectors) do
|
239
|
+
{
|
240
|
+
model: SimpleModel,
|
241
|
+
columns: [:other_model_id],
|
242
|
+
tracks: {
|
243
|
+
other_model: {
|
244
|
+
model: OtherModel,
|
245
|
+
columns: [:id, :name]
|
246
|
+
}
|
247
|
+
}
|
248
|
+
}
|
249
|
+
end
|
250
|
+
it_behaves_like 'a proper selector'
|
251
|
+
end
|
175
252
|
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
# end
|
253
|
+
context 'for a property that requires all fields from an association' do
|
254
|
+
let(:fields) { {everything_from_parent: true} }
|
255
|
+
let(:selectors) do
|
256
|
+
{
|
257
|
+
model: SimpleModel,
|
258
|
+
columns: [:parent_id],
|
259
|
+
tracks: {
|
260
|
+
parent: {
|
261
|
+
model: ParentModel,
|
262
|
+
columns: [:*]
|
263
|
+
}
|
264
|
+
}
|
265
|
+
}
|
266
|
+
end
|
267
|
+
it_behaves_like 'a proper selector'
|
268
|
+
end
|
269
|
+
end
|
194
270
|
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
271
|
+
context 'required extra select fields due to associations' do
|
272
|
+
context 'many_to_one' do
|
273
|
+
let(:fields) { {other_model: true} }
|
274
|
+
let(:selectors) do
|
275
|
+
{
|
276
|
+
model: SimpleModel,
|
277
|
+
columns: [:other_model_id], # FK of the other_model association
|
278
|
+
tracks: {
|
279
|
+
other_model: {
|
280
|
+
columns: [:id],
|
281
|
+
model: OtherModel
|
282
|
+
}
|
283
|
+
}
|
284
|
+
}
|
285
|
+
end
|
286
|
+
it_behaves_like 'a proper selector'
|
287
|
+
end
|
288
|
+
context 'one_to_many' do
|
289
|
+
let(:resource) { ParentResource }
|
290
|
+
let(:fields) { {simple_children: true} }
|
291
|
+
let(:selectors) do
|
292
|
+
{
|
293
|
+
model: ParentModel,
|
294
|
+
columns: [:id], # No FKs in the source model for one_to_many
|
295
|
+
tracks: {
|
296
|
+
simple_children: {
|
297
|
+
columns: [:parent_id],
|
298
|
+
model: SimpleModel
|
299
|
+
}
|
300
|
+
}
|
301
|
+
}
|
302
|
+
end
|
303
|
+
it_behaves_like 'a proper selector'
|
304
|
+
end
|
305
|
+
context 'many_to_many' do
|
306
|
+
let(:resource) { OtherResource }
|
307
|
+
let(:fields) { {simple_models: true} }
|
308
|
+
let(:selectors) do
|
309
|
+
{
|
310
|
+
model: OtherModel,
|
311
|
+
columns: [:id], #join key in the source model for many_to_many (where the middle table points to)
|
312
|
+
tracks: {
|
313
|
+
simple_models: {
|
314
|
+
columns: [:id], #join key in the target model for many_to_many (where the middle table points to)
|
315
|
+
model: SimpleModel
|
316
|
+
}
|
317
|
+
}
|
318
|
+
}
|
319
|
+
end
|
320
|
+
it_behaves_like 'a proper selector'
|
321
|
+
end
|
214
322
|
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
# let(:properties) { {id: true, slug: true} }
|
219
|
-
# let(:expected_selectors) do
|
220
|
-
# {
|
221
|
-
# PostModel => {
|
222
|
-
# select: Set.new([:id, :slug, :title]),
|
223
|
-
# track: Set.new
|
224
|
-
# }
|
225
|
-
# }
|
226
|
-
# end
|
227
|
-
# it 'generates the correct set of selectors' do
|
228
|
-
# generator.selectors.should eq expected_selectors
|
229
|
-
# end
|
230
|
-
# end
|
231
|
-
|
232
|
-
# context 'with a property without the :through option' do
|
233
|
-
# let(:resource) { UserResource }
|
234
|
-
# let(:properties) { {blogs_summary: {size: true}} }
|
235
|
-
# let(:expected_selectors) do
|
236
|
-
# {
|
237
|
-
# BlogModel => {
|
238
|
-
# select: Set.new([:owner_id]),
|
239
|
-
# track: Set.new()
|
240
|
-
# },
|
241
|
-
# UserModel => {
|
242
|
-
# select: Set.new([:id]),
|
243
|
-
# track: Set.new([:blogs])
|
244
|
-
# }
|
245
|
-
# }
|
246
|
-
# end
|
247
|
-
# it 'ignores any subsequent fields when generating selectors' do
|
248
|
-
# generator.selectors.should eq expected_selectors
|
249
|
-
# end
|
250
|
-
# end
|
251
|
-
|
252
|
-
# context 'for a property with no dependencies' do
|
253
|
-
# let(:properties) { {id: true, kind: true} }
|
254
|
-
# let(:expected_selectors) do
|
255
|
-
# {
|
256
|
-
# BlogModel => {
|
257
|
-
# select: Set.new([:id]),
|
258
|
-
# track: Set.new()
|
259
|
-
# }
|
260
|
-
# }
|
261
|
-
# end
|
262
|
-
# it 'generates the correct set of selectors' do
|
263
|
-
# generator.selectors.should eq expected_selectors
|
264
|
-
# end
|
265
|
-
# end
|
266
|
-
|
267
|
-
# context 'with large set of properties' do
|
268
|
-
|
269
|
-
# let(:properties) do
|
270
|
-
# {
|
271
|
-
# display_name: true,
|
272
|
-
# owner: {
|
273
|
-
# id: true,
|
274
|
-
# full_name: true,
|
275
|
-
# blogs_summary: {href: true, size: true},
|
276
|
-
# main_blog: {id: true},
|
277
|
-
# },
|
278
|
-
# administrator: {id: true, full_name: true}
|
279
|
-
# }
|
280
|
-
# end
|
281
|
-
|
282
|
-
# let(:expected_selectors) do
|
283
|
-
# {
|
284
|
-
# BlogModel=> {
|
285
|
-
# select: Set.new([:id, :name, :owner_id, :administrator_id]),
|
286
|
-
# track: Set.new([:owner, :administrator])
|
287
|
-
# },
|
288
|
-
# UserModel=> {
|
289
|
-
# select: Set.new([:id, :first_name, :last_name, :main_blog_id]),
|
290
|
-
# track: Set.new([:blogs, :main_blog])
|
291
|
-
# }
|
292
|
-
# }
|
293
|
-
# end
|
294
|
-
|
295
|
-
# it 'generates the correct set of selectors' do
|
296
|
-
# generator.selectors.should eq(expected_selectors)
|
297
|
-
# end
|
298
|
-
|
299
|
-
# end
|
300
|
-
|
301
|
-
# end
|
323
|
+
end
|
324
|
+
end
|
325
|
+
end
|