Skip to content

Instantly share code, notes, and snippets.

View amkisko's full-sized avatar
😺
Hymyillen suora selkä!

Andrei Makarov amkisko

😺
Hymyillen suora selkä!
  • Kisko Labs
  • Helsinki, Finland
View GitHub Profile
@amkisko
amkisko / base_object_with_listable.rb
Last active February 3, 2020 08:52
GraphQL Ruby Pagination approach
module Types
class BaseObject < GraphQL::Schema::Object
field_class Types::BaseField
def self.listable(name, object_type, name_suffix: "", override_options: {})
list_type = Class.new(Types::BaseObject) do
graphql_name "#{name}#{name_suffix}"
field :rows, [object_type], null: true
field :totalCount, Integer, null: true
end
@amkisko
amkisko / safe_order.rb
Last active February 26, 2020 08:04
ActiveRecord safe order
module ActiveRecord
class Relation
def safe_order(field, direction = "asc")
direction = direction.downcase.to_sym
# NOTE: https://gist.github.com/amkisko/aec1283b9797e94348f89e3a2b934bc0
raise Error::CustomError, code: "QUERY_ORDER_INVALID_DIRECTION" unless %i[asc desc].include?(direction)
# field = connection.quote_table_name(field)
order(field => direction)
end
@amkisko
amkisko / _rails_app_code_tests
Created March 6, 2020 16:15
Rails app tests
* Brakeman test
* Bundle audit test
* Rubocop test
@amkisko
amkisko / bin_pre-commit
Last active March 6, 2020 16:19
Sample pre-commit ruby script
#!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
lib_dir = File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
$LOAD_PATH << lib_dir unless $LOAD_PATH.include?(lib_dir)
# NOTE: https://github.com/piotrmurach/tty-command
require "tty-command"
@amkisko
amkisko / rake_task_invoke.rb
Created March 19, 2020 09:55
Invoke rake task from Rails console
require "rake"
RailsApp::Application.load_tasks
Rake::Task["my_task"].invoke
@amkisko
amkisko / efilter.rb
Last active April 6, 2020 20:14
ActiveRecord extended filter for relations
class ActiveRecord::Relation
def efilter(filter)
where(ExtFilter.new(self, filter).to_s)
end
end
class ExtFilter
attr_accessor :relation
attr_accessor :conn
attr_accessor :filter
@amkisko
amkisko / list_extension.rb
Last active April 6, 2020 20:17
GraphQL Ruby List extension
class FieldExtensions::ListExtension < GraphQL::Schema::FieldExtension
def apply
object_type = field.type.unwrap
list_type_name = object_type.graphql_name
namespace = options[:namespace] || "default"
list_type =
Class.new(Objects::BaseObject) do
self.graphql_name("#{namespace}_#{list_type_name}_list")
self.field(:rows, [object_type], null: true)
@amkisko
amkisko / basic_controller.rb
Created April 6, 2020 20:20
Basic controller for Rails app with graphql-ruby
class ApplicationController < ActionController::Base
include Pundit
skip_forgery_protection
rescue_from(StandardError) do |err|
log_error(err)
if Rails.env.development?
render_error err
else
render_error Error::InternalServerError.new(code: err.class.to_s.sub(/:+/, "_").underscore.upcase)
@amkisko
amkisko / basic_schema.rb
Last active April 6, 2020 20:21
Basic schema for graphql-ruby
class BasicSchema < GraphQL::Schema
default_max_page_size 25
use(GraphQL::Execution::Interpreter)
use(GraphQL::Analysis::AST)
query_analyzer(QueryAnalyzer)
use(GraphQL::Execution::Errors)
use(BatchLoader::GraphQL)
@amkisko
amkisko / exceptions_middleware.rb
Created April 6, 2020 20:23
Exceptions middleware for handling low-level errors for Rails with graphql-ruby app
# NOTE: Add `config.exceptions_app = ExceptionsMiddleware.new` to application.rb
class ExceptionsMiddleware
def initialize
end
def call(env)
error = env["action_dispatch.exception"]
request = ActionDispatch::Request.new(env)