Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize modules #99

Merged
merged 3 commits into from
Mar 21, 2024
Merged
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
38 changes: 0 additions & 38 deletions lib/stretchy/common.rb

This file was deleted.

40 changes: 40 additions & 0 deletions lib/stretchy/model/common.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Stretchy
module Model
module Common
extend ActiveSupport::Concern

def highlights_for(attribute)
highlights[attribute.to_s]
end

class_methods do

# Set the default sort key to be used in sort operations
#
def default_sort_key(field = nil)
@default_sort_key = field unless field.nil?
@default_sort_key
end

def default_size(size = nil)
@default_size = size unless size.nil?
@default_size
end

def default_pipeline(pipeline = nil)
@default_pipeline = pipeline.to_s unless pipeline.nil?
@default_pipeline
end

private

# Return a Relation instance to chain queries
#
def relation
Relation.create(self, {})
end

end
end
end
end
45 changes: 45 additions & 0 deletions lib/stretchy/model/persistence.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Stretchy
module Model
module Persistence
extend ActiveSupport::Concern

class_methods do
def create(*args)
self.new(*args).save
end
end

def save
run_callbacks :save do
if new_record?
run_callbacks :create do
response = self.class.gateway.save(self.attributes)
self.id = response['_id']
end
else
self.class.gateway.save(self.attributes)
end
self
end
end

def destroy
run_callbacks :destroy do
delete
end
end

def delete
self.class.gateway.delete(self.id)["result"] == 'deleted'
end

def update(*args)
run_callbacks :update do
self.assign_attributes(*args)
self.save
end
end

end
end
end
17 changes: 17 additions & 0 deletions lib/stretchy/model/refreshable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Stretchy
module Model
module Refreshable
extend ActiveSupport::Concern

included do
after_save :refresh_index
after_destroy :refresh_index
end

def refresh_index
self.class.refresh_index!
end

end
end
end
53 changes: 0 additions & 53 deletions lib/stretchy/null_relation.rb

This file was deleted.

43 changes: 0 additions & 43 deletions lib/stretchy/persistence.rb

This file was deleted.

6 changes: 3 additions & 3 deletions lib/stretchy/querying.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Stretchy
module Querying
delegate :first, :first!, :last, :last!, to: :all
delegate :exists?, :any?, :many?, :includes, to: :all
delegate :rewhere, :eager_load, :create_with, :none, :unscope, to: :all
delegate :routing, :search_options, to: :all
delegate :rewhere, :eager_load, :create_with, :unscoped, to: :all

delegate *Stretchy::Relations::FinderMethods::METHODS, to: :all
delegate *Stretchy::Relations::SearchOptionMethods::METHODS, to: :all
delegate *Stretchy::Relations::QueryMethods.registry, to: :all
delegate *Stretchy::Relations::AggregationMethods.registry, to: :all

Expand Down
8 changes: 4 additions & 4 deletions lib/stretchy/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ def self.inherited(base)
include ActiveModel::Serializers::JSON

include Stretchy::Model::Callbacks
include Stretchy::Model::Common
include Stretchy::Model::Persistence
include Stretchy::Model::Refreshable
include Stretchy::Indexing::Bulk
include Stretchy::Persistence
include Stretchy::Associations
include Stretchy::Refreshable
include Stretchy::Common
include Stretchy::Scoping
include Stretchy::Relations::Scoping
include Stretchy::Utils
include Stretchy::SharedScopes
include Stretchy::Attributes
Expand Down
15 changes: 0 additions & 15 deletions lib/stretchy/refreshable.rb

This file was deleted.

20 changes: 2 additions & 18 deletions lib/stretchy/relations/finder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Relations

module FinderMethods

METHODS = [:first, :first!, :last, :last!]

def first
return results.first if @loaded
spawn.first!.results.first
Expand Down Expand Up @@ -39,24 +41,6 @@ def last!
self
end

# size is not permitted to the count API but queries are.
#
# if a query is supplied with `.count` then the user wants a count of the results
# matching the query
#
# however, the default_size is used to limit the number of results returned
# so we remove the size from the query and then call `.count` API
#
# but if the user supplies a limit, then we should assume they want a count of the results
# which could lead to some confusion
# suppose the user calls `.size(100).count` and the default_size is 100
# if we remove size from the query, then the count could be greater than 100
#
# I think the best way to handle this is to remove the size from the query only if it was
# applied by the default_size
# If the user supplies a limit, then we should assume they want a count of the results
#
# if size is called with a limit,
def count
return results.count if @loaded || @values[:size].present?
spawn.count!
Expand Down
55 changes: 55 additions & 0 deletions lib/stretchy/relations/null_relation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

module Stretchy
module Relations
module NullRelation # :nodoc:
def pluck(*column_names)
[]
end

def delete_all
0
end

def update_all(_updates)
0
end

def delete(_id_or_array)
0
end

def empty?
true
end

def none?
true
end

def any?
false
end

def one?
false
end

def many?
false
end

def exists?(_conditions = :none)
false
end

def or(other)
other.spawn
end

def exec_queries
@records = OpenStruct.new(klass: NullRelation, total: 0, results: []).freeze
end
end
end
end
4 changes: 2 additions & 2 deletions lib/stretchy/relations/query_methods/none.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ module None

# Returns a chainable relation with zero records.
def none
extending(NullRelation)
extending(Stretchy::Relations::NullRelation)
end

def none! # :nodoc:
extending!(NullRelation)
extending!(Stretchy::Relations::NullRelation)
end

QueryMethods.register!(:none)
Expand Down
Loading
Loading