-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
622 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class ActiveCallDecorator < ApplicationDecorator | ||
def self.object_class_namespace | ||
'RealtimeData' | ||
end | ||
|
||
RealtimeData::ActiveCall.association_types.each do |name, foreign_key:, **_| | ||
define_method("#{name}_link") do | ||
record = model.public_send(name) | ||
record ? h.auto_link(record) : model.public_send(foreign_key) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationDecorator < Draper::Decorator | ||
include Rails.application.routes.url_helpers | ||
delegate_all | ||
|
||
def self.object_class_name | ||
return nil if name.nil? || name.demodulize !~ /.+Decorator$/ | ||
|
||
class_name = name.chomp('Decorator') | ||
namespace = object_class_namespace | ||
namespace.blank? ? class_name : "#{namespace}::#{class_name}" | ||
end | ||
|
||
def self.object_class_namespace | ||
nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# frozen_string_literal: true | ||
|
||
module QueryBuilder | ||
class Base | ||
extend Forwardable | ||
|
||
VALUE_METHODS = %i[size first last map each collect].freeze | ||
|
||
instance_delegate VALUE_METHODS => :to_a | ||
|
||
class << self | ||
def define_chainable(name, &block) | ||
define_method(name) do |*args| | ||
perform_chainable(*args, &block) | ||
end | ||
end | ||
end | ||
|
||
define_chainable :where do |conditions| | ||
filter_values.merge!(conditions.symbolize_keys) | ||
end | ||
|
||
define_chainable :includes do |*values| | ||
old_includes = include_values.dup | ||
old_includes_nested = old_includes.extract_options! | ||
new_includes = values.flatten | ||
new_includes_nested = new_includes.extract_options! | ||
simple = (old_includes + new_includes).uniq.map(&:to_sym) | ||
nested = old_includes_nested.deep_merge(new_includes_nested) | ||
self.include_values = simple + [nested] | ||
end | ||
|
||
define_chainable :none do | ||
self.is_none = true | ||
end | ||
|
||
def initialize | ||
@filter_values = {} | ||
@include_values = [] | ||
@is_none = false | ||
end | ||
|
||
def dup | ||
new_instance = self.class.new(*dup_params) | ||
new_instance.filter_values = filter_values.dup | ||
new_instance.include_values = include_values.dup | ||
new_instance.is_none = is_none | ||
new_instance | ||
end | ||
|
||
def to_a | ||
return @to_a if defined?(@to_a) | ||
|
||
@to_a = find_collection | ||
end | ||
|
||
def find(id) | ||
find_record(id) | ||
end | ||
|
||
def reset | ||
remove_instance_variable(:"@to_a") | ||
self | ||
end | ||
|
||
def all | ||
self | ||
end | ||
|
||
protected | ||
|
||
attr_accessor :filter_values, :include_values, :is_none | ||
|
||
private | ||
|
||
def dup_params | ||
[] | ||
end | ||
|
||
def find_record(_id) | ||
raise NotImplementedError, "implement #find_record method in #{self.class}" | ||
end | ||
|
||
def find_collection | ||
raise NotImplementedError, "implement #find_collection method in #{self.class}" | ||
end | ||
|
||
def perform_chainable(*args, &block) | ||
new_instance = dup | ||
new_instance.instance_exec(*args, &block) | ||
new_instance | ||
end | ||
end | ||
end |
Oops, something went wrong.