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

Change Default Page Method #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
45 changes: 18 additions & 27 deletions lib/cursor/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,33 @@ module Cursor
# config.default_per_page = 10
# end
def self.configure(&block)
yield @config ||= Cursor::Configuration.new
yield config
end

# Global settings for Cursor
def self.config
@config
@config ||= Cursor::Configuration.new
end

# need a Class for 3.0
class Configuration #:nodoc:
include ActiveSupport::Configurable
config_accessor :default_per_page
config_accessor :max_per_page
config_accessor :page_method_name
config_accessor :before_param_name
config_accessor :after_param_name

config_accessor :default_per_page do
25
end
config_accessor :max_per_page do
nil
end
config_accessor :page_method_name do
:cursor
end
config_accessor :before_param_name do
:before
end
config_accessor :after_param_name do
:after
end

def before_param_name
config.before_param_name.respond_to?(:call) ? config.before_param_name.call : config.before_param_name
Expand All @@ -30,25 +41,5 @@ def before_param_name
def after_param_name
config.after_param_name.respond_to?(:call) ? config.after_param_name.call : config.after_param_name
end


# define param_name writer (copied from AS::Configurable)
writer, line = 'def before_param_name=(value); config.before_param_name = value; end', __LINE__
singleton_class.class_eval writer, __FILE__, line
class_eval writer, __FILE__, line

writer, line = 'def after_param_name=(value); config.after_param_name = value; end', __LINE__
singleton_class.class_eval writer, __FILE__, line
class_eval writer, __FILE__, line

end

# this is ugly. why can't we pass the default value to config_accessor...?
configure do |config|
config.default_per_page = 25
config.max_per_page = nil
config.page_method_name = :page
config.before_param_name = :before
config.after_param_name = :after
end
end
15 changes: 7 additions & 8 deletions lib/cursor/models/active_record_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
module Cursor
module ActiveRecordExtension
extend ActiveSupport::Concern
included do
# Future subclasses will pick up the model extension
class << self
def inherited_with_cursor(kls) #:nodoc:
inherited_without_cursor kls
kls.send(:include, Cursor::ActiveRecordModelExtension) if kls.superclass == ::ActiveRecord::Base
end
alias_method_chain :inherited, :cursor

module ClassMethods
def inherited(kls)
super kls
kls.send(:include, Cursor::ActiveRecordModelExtension) if kls.superclass == ::ActiveRecord::Base
end
end

included do
# Existing subclasses pick up the model extension as well
self.descendants.each do |kls|
kls.send(:include, Cursor::ActiveRecordModelExtension) if kls.superclass == ::ActiveRecord::Base
Expand Down
3 changes: 1 addition & 2 deletions lib/cursor/models/active_record_model_extension.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

module Cursor
module ActiveRecordModelExtension
extend ActiveSupport::Concern

included do
self.send(:include, Cursor::ConfigurationMethods)
include Cursor::ConfigurationMethods

# Fetch the values at the specified page edge
# Model.page(after: 5)
Expand Down