Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ require "bundler/gem_tasks"
require 'fileutils'
require 'rake/testtask'

desc "Run tests"
task :test => [ :test_standard_app, :test_order_param_name ]

# Test Task
Rake::TestTask.new do |t|
t.libs << 'lib' << 'test'
t.test_files = FileList['test/standard_api/**/*_test.rb']
# t.warning = true
# t.verbose = true
Rake::TestTask.new("test_standard_app") do |t|
t.libs << 'lib' << 'test'
t.test_files = FileList.new('test/standard_api/**/*_test.rb') do |fl|
fl.exclude('test/standard_api/controller/custom_order_test.rb')
end
end

Rake::TestTask.new("test_order_param_name") do |t|
t.libs << 'lib' << 'test'
t.test_files = FileList['test/standard_api/controller/custom_order_test.rb']
end

Rake::TestTask.new('benchmark') do |t|
Expand Down
30 changes: 15 additions & 15 deletions lib/standard_api/controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module StandardAPI
module Controller

delegate :preloadables, :model_partial, to: :helpers
delegate :preloadables, :model_partial, :order_param_name, to: :helpers

def self.included(klass)
klass.helper_method :includes, :orders, :model, :models, :resource_limit,
Expand Down Expand Up @@ -144,11 +144,11 @@ def add_resource
"Relationship between #{resource.class.name} and #{subresource.class.name} violates unique constraints"
]}, status: :bad_request
end

def create_resource
resource = resources.find(params[:id])
association = resource.association(params[:relationship])

subresource_params = if self.respond_to?("filter_#{model_name(association.klass)}_params", true)
self.send("filter_#{model_name(association.klass)}_params", params[model_name(association.klass)], id: params[:id])
elsif self.respond_to?("#{association.klass.model_name.singular}_params", true)
Expand All @@ -158,9 +158,9 @@ def create_resource
else
ActionController::Parameters.new
end

subresource = association.klass.new(subresource_params)

result = case association
when ActiveRecord::Associations::CollectionAssociation
association.concat(subresource)
Expand All @@ -182,7 +182,7 @@ def mask
hash[key] = mask_for(key)
end
end

# Override if you want to support masking
def mask_for(table_name)
# case table_name
Expand Down Expand Up @@ -282,7 +282,7 @@ def resources

query
end

def nested_includes(model, attributes)
includes = {}
attributes&.each do |key, value|
Expand All @@ -299,11 +299,11 @@ def includes
else
{}
end

if (action_name == 'create' || action_name == 'update') && model && params.has_key?(model.model_name.singular)
@includes.reverse_merge!(nested_includes(model, params[model.model_name.singular].to_unsafe_h))
end

@includes
end

Expand All @@ -318,11 +318,11 @@ def default_orders
def orders
exluded_required_orders = required_orders.map(&:to_s)

case params[:order]
case params[order_param_name]
when Hash, ActionController::Parameters
exluded_required_orders -= params[:order].keys.map(&:to_s)
exluded_required_orders -= params[order_param_name].keys.map(&:to_s)
when Array
params[:order].flatten.each do |v|
params[order_param_name].flatten.each do |v|
case v
when Hash, ActionController::Parameters
exluded_required_orders -= v.keys.map(&:to_s)
Expand All @@ -331,14 +331,14 @@ def orders
end
end
when String
exluded_required_orders.delete(params[:order])
exluded_required_orders.delete(params[order_param_name])
end

if !exluded_required_orders.empty?
params[:order] = exluded_required_orders.unshift(params[:order])
params[order_param_name] = exluded_required_orders.unshift(params[order_param_name])
end

@orders ||= StandardAPI::Orders.sanitize(params[:order] || default_orders, model_orders | required_orders)
@orders ||= StandardAPI::Orders.sanitize(params[order_param_name] || default_orders, model_orders | required_orders)
end

def excludes
Expand Down
14 changes: 9 additions & 5 deletions lib/standard_api/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module StandardAPI
module Helpers

def serialize_attribute(json, record, name, type)
value = record.send(name)

json.set! name, type == :binary ? value&.unpack1('H*') : value
end

Expand All @@ -16,7 +16,7 @@ def preloadables(record, includes)
when true
preloads[key] = value
when Hash, ActiveSupport::HashWithIndifferentAccess
if !value.keys.any? { |x| ['when', 'where', 'limit', 'offset', 'order', 'distinct'].include?(x) }
if !value.keys.any? { |x| ['when', 'where', 'limit', 'offset', order_param_name, 'distinct'].include?(x) }
if !reflection.polymorphic?
preloads[key] = preloadables_hash(reflection.klass, value)
end
Expand All @@ -37,7 +37,7 @@ def preloadables_hash(klass, iclds)
when true
preloads[key] = value
when Hash, ActiveSupport::HashWithIndifferentAccess
if !value.keys.any? { |x| ['when', 'where', 'limit', 'offset', 'order', 'distinct'].include?(x) }
if !value.keys.any? { |x| ['when', 'where', 'limit', 'offset', order_param_name, 'distinct'].include?(x) }
if !reflection.polymorphic?
preloads[key] = preloadables_hash(reflection.klass, value)
end
Expand Down Expand Up @@ -117,7 +117,7 @@ def association_cache_key(record, relation, subincludes)
end

def cached_at_columns_for_includes(includes)
includes.select { |k,v| !['when', 'where', 'limit', 'order', 'distinct', 'distinct_on'].include?(k) }.map do |k, v|
includes.select { |k,v| !['when', 'where', 'limit', order_param_name, 'distinct', 'distinct_on'].include?(k) }.map do |k, v|
["#{k}_cached_at"] + cached_at_columns_for_includes(v).map { |v2| "#{k}_#{v2}" }
end.flatten
end
Expand Down Expand Up @@ -199,5 +199,9 @@ def json_column_type(sql_type)
end
end

def order_param_name
Rails.application.config.standard_api.order_param_name.to_s
end

end
end
8 changes: 6 additions & 2 deletions lib/standard_api/includes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module StandardAPI
module Includes

def self.order_param_name
Rails.application.config.standard_api.order_param_name.to_s
end

# :x => { x: {} }
# [:x, :y] => { x: {}, y: {} }
# [ { x: true }, { y: true } ] => { x: {}, y: {} }
Expand All @@ -18,7 +22,7 @@ def self.normalize(includes)
when Hash, ActionController::Parameters
includes.each_pair do |k, v|
normalized[k] = case k.to_s
when 'when', 'where', 'order'
when 'when', 'where', order_param_name
case v
when Array
v.map do |x|
Expand Down Expand Up @@ -81,7 +85,7 @@ def self.sanitize(includes, permit, normalized=false)
includes.each do |k, v|
permitted[k] = if permit.has_key?(k)
sanitize(v, permit[k] || {}, true)
elsif ['limit', 'when', 'where', 'order', 'distinct', 'distinct_on'].include?(k.to_s)
elsif ['limit', 'when', 'where', order_param_name, 'distinct', 'distinct_on'].include?(k.to_s)
v
else
raise StandardAPI::UnpermittedParameters.new([k])
Expand Down
3 changes: 3 additions & 0 deletions lib/standard_api/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module StandardAPI
class Railtie < ::Rails::Railtie

config.standard_api = ActiveSupport::OrderedOptions.new
config.standard_api.order_param_name = :order

initializer 'standardapi', :before => :set_autoload_paths do |app|
if app.root.join('app', 'controllers', 'acl').exist?
ActiveSupport::Inflector.inflections(:en) do |inflect|
Expand Down
4 changes: 2 additions & 2 deletions lib/standard_api/views/application/_record.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ record.attribute_names.each do |name|
end

includes.each do |inc, subinc|
next if ["limit", "offset", "order", "when", "where", "distinct", "distinct_on"].include?(inc)
next if ["limit", "offset", order_param_name, "when", "where", "distinct", "distinct_on"].include?(inc)

case association = record.class.reflect_on_association(inc)
when ActiveRecord::Reflection::AbstractReflection
Expand All @@ -21,7 +21,7 @@ includes.each do |inc, subinc|

sub_records = sub_records.limit(subinc['limit']) if subinc['limit']
sub_records = sub_records.offset(subinc['offset']) if subinc['offset']
sub_records = sub_records.reorder(subinc['order']) if subinc['order']
sub_records = sub_records.reorder(subinc[order_param_name]) if subinc[order_param_name]
sub_records = sub_records.filter(subinc['where']) if subinc['where']
sub_records = sub_records.distinct if subinc['distinct']
sub_records = sub_records.distinct_on(subinc['distinct_on']) if subinc['distinct_on']
Expand Down
4 changes: 2 additions & 2 deletions lib/standard_api/views/application/_record.streamer
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ json.object! do
end

includes.each do |inc, subinc|
next if ["limit", "offset", "order", "when", "where", "distinct", "distinct_on"].include?(inc)
next if ["limit", "offset", order_param_name, "when", "where", "distinct", "distinct_on"].include?(inc)

case association = record.class.reflect_on_association(inc)
when ActiveRecord::Reflection::AbstractReflection
Expand All @@ -22,7 +22,7 @@ json.object! do

sub_records = sub_records.limit(subinc["limit"]) if subinc["limit"]
sub_records = sub_records.offset(subinc["offset"]) if subinc["offset"]
sub_records = sub_records.reorder(subinc["order"]) if subinc["order"]
sub_records = sub_records.reorder(subinc[order_param_name]) if subinc[order_param_name]
sub_records = sub_records.filter(subinc["where"]) if subinc["where"]
sub_records = sub_records.distinct if subinc["distinct"]
sub_records = sub_records.distinct_on(subinc["distinct_on"]) if subinc["distinct_on"]
Expand Down
1 change: 1 addition & 0 deletions test/standard_api/caching_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'standard_api/test_app'
require 'standard_api/test_helper'

class AccountsControllerTest < ActionDispatch::IntegrationTest
Expand Down
47 changes: 47 additions & 0 deletions test/standard_api/controller/custom_order_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'standard_api/sort_test_app'
require 'standard_api/test_helper'

class ControllerCustomOrderTest < ActionDispatch::IntegrationTest

# = Including an invalid include

test "Controller#index with order name set sort" do
account = create(:account)
order = create(:order, account: account)
account.update(order: order)

get "/account", params: { include: { order: true, orders: { account: { order: true } } } }, as: :json
json = JSON.parse(response.body)

assert_equal [
{
id: order.id,
account_id: account.id,
name: order.name,
price: order.price,
account: {
id: account.id,
name: account.name,
order_id: account.order_id,
created_at: account.created_at,
updated_at: account.updated_at,
order: {
id: order.id,
account_id: account.id,
name: order.name,
price: order.price
}
}
}
].to_json, json["orders"].to_json

assert_equal(
{
id: order.id,
account_id: account.id,
name: order.name,
price: order.price
}.stringify_keys, json["order"])
end

end
1 change: 1 addition & 0 deletions test/standard_api/controller/include_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'standard_api/test_app'
require 'standard_api/test_helper'

class ControllerIncludesTest < ActionDispatch::IntegrationTest
Expand Down
3 changes: 2 additions & 1 deletion test/standard_api/controller/subresource_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'standard_api/test_app'
require 'standard_api/test_helper'

class ControllerSubresourceTest < ActionDispatch::IntegrationTest

# add_resource
test 'Controller#add_resource with has_many' do
property = create(:property, photos: [])
Expand Down
1 change: 1 addition & 0 deletions test/standard_api/helpers_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'standard_api/test_app'
require 'standard_api/test_helper'

class HelpersTest < ActionView::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/standard_api/nested_attributes/belongs_to_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'standard_api/test_app'
require 'standard_api/test_helper'

module NestedAttributes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'standard_api/test_app'
require 'standard_api/test_helper'

module NestedAttributes
Expand Down
1 change: 1 addition & 0 deletions test/standard_api/nested_attributes/has_many_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'standard_api/test_app'
require 'standard_api/test_helper'

module NestedAttributes
Expand Down
1 change: 1 addition & 0 deletions test/standard_api/nested_attributes/has_one_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'standard_api/test_app'
require 'standard_api/test_helper'

module NestedAttributes
Expand Down
1 change: 1 addition & 0 deletions test/standard_api/performance.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'standard_api/test_app'
require 'standard_api/test_helper'
require "benchmark/ips"

Expand Down
1 change: 1 addition & 0 deletions test/standard_api/route_helpers_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'standard_api/test_app'
require 'standard_api/test_helper'

class RouteHelpersTest < ActionDispatch::IntegrationTest
Expand Down
Loading