Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jaydorsey
Last active July 26, 2019 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaydorsey/b6f290fa2f1b7a002b6e734033373ff7 to your computer and use it in GitHub Desktop.
Save jaydorsey/b6f290fa2f1b7a002b6e734033373ff7 to your computer and use it in GitHub Desktop.
jsonapi-resources bug report
begin
require 'bundler/inline'
require 'bundler'
require 'pry'
require 'pry-byebug'
rescue LoadError => e
STDERR.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true, ui: ENV['SILENT'] ? Bundler::UI::Silent.new : Bundler::UI::Shell.new) do
source 'https://rubygems.org'
gem 'rails', require: false
gem 'pg'
gem 'sqlite3', platform: :mri
gem 'redis'
gem 'jsonapi-resources', tag: 'v.0.9.5', require: false
end
# prepare active_record database
require 'active_record'
class NullLogger < Logger
def initialize(*_args)
end
def add(*_args, &_block)
end
end
# createdb jsonapi_test first
ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'jsonapi_test')
# ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = ENV['SILENT'] ? NullLogger.new : Logger.new(STDOUT)
ActiveRecord::Migration.verbose = !ENV['SILENT']
ActiveRecord::Schema.define do
# Add your schema here
create_table :contacts, force: true do |t|
t.string :name
t.timestamps
end
end
# create models
class Contact < ActiveRecord::Base
end
# prepare rails app
require 'action_controller/railtie'
# require 'action_view/railtie'
require 'jsonapi-resources'
class ApplicationController < ActionController::Base
end
# prepare jsonapi resources and controllers
class ContactsController < ApplicationController
include JSONAPI::ActsAsResourceController
end
class ContactResource < JSONAPI::Resource
attribute :name
# Disabling this fixes everything
caching
filter(
:name,
apply: ->(records, values, _options) {
records.where('lower(contacts.name) LIKE ?', values.first.downcase).distinct
}
)
end
class TestApp < Rails::Application
config.root = File.dirname(__FILE__)
config.logger = ENV['SILENT'] ? NullLogger.new : Logger.new(STDOUT)
Rails.logger = config.logger
secrets.secret_token = 'secret_token'
secrets.secret_key_base = 'secret_key_base'
config.eager_load = false
config.action_controller.perform_caching = true
config.cache_store = :redis_cache_store, { url: 'redis://127.0.0.1:6379/0' }
end
# initialize app
Rails.application.initialize!
JSONAPI.configure do |config|
config.json_key_format = :underscored_key
config.route_format = :underscored_key
config.resource_cache = Rails.cache
end
# draw routes
Rails.application.routes.draw do
jsonapi_resources :contacts, only: [:index, :create]
end
# prepare tests
require 'minitest/autorun'
require 'rack/test'
# Replace this with the code necessary to make your test fail.
class BugTest < Minitest::Test
include Rack::Test::Methods
def json_api_headers
{'Accept' => JSONAPI::MEDIA_TYPE, 'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE}
end
def test_filter
Contact.create! name: 'John Doe'
Contact.create! name: 'Sally Doe'
get '/contacts?filter%5Bname%5D=John', nil, json_api_headers
assert last_response.ok?
end
def test_sort
Contact.create! name: 'John Doe'
Contact.create! name: 'Sally Doe'
get '/contacts?sort=name', nil, json_api_headers
assert last_response.ok?
end
def test_filter_and_sort
Contact.create! name: 'John Doe'
Contact.create! name: 'Sally Doe'
get '/contacts?filter%5Bname%5D=John&sort=name', nil, json_api_headers
assert last_response.ok?
end
private
def app
Rails.application
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment