mongoid_orderable 5.0.0 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,17 +1,14 @@
1
1
  module Mongoid::Orderable
2
2
  module Errors
3
3
  class InvalidTargetPosition < Mongoid::Orderable::Errors::MongoidOrderableError
4
- def initialize value
4
+ def initialize(value)
5
5
  super _compose_message(value)
6
6
  end
7
7
 
8
8
  private
9
- def _compose_message value
10
- if ::Mongoid::Compatibility::Version.mongoid2?
11
- translate 'invalid_target_position', { :value => value.inspect }
12
- else
13
- compose_message 'invalid_target_position', { :value => value.inspect }
14
- end
9
+
10
+ def _compose_message(value)
11
+ compose_message 'invalid_target_position', value: value.inspect
15
12
  end
16
13
  end
17
14
  end
@@ -1,14 +1,6 @@
1
1
  module Mongoid::Orderable
2
2
  module Errors
3
3
  class MongoidOrderableError < ::Mongoid::Errors::MongoidError
4
-
5
- if ::Mongoid::Compatibility::Version.mongoid2?
6
- def translate key, options
7
- [:message, :summary, :resolution].map do |section|
8
- ::I18n.translate "#{BASE_KEY}.#{key}.#{section}", options
9
- end.join ' '
10
- end
11
- end
12
4
  end
13
5
  end
14
6
  end
@@ -2,7 +2,6 @@ module Mongoid
2
2
  module Orderable
3
3
  module Generator
4
4
  module Helpers
5
-
6
5
  def generate_orderable_helpers
7
6
  self_class = klass
8
7
 
@@ -19,11 +18,10 @@ module Mongoid
19
18
  eos
20
19
 
21
20
  generate_method(:orderable_inherited_class) do
22
- self_class.orderable_configurations.any?{ |col, conf| conf[:inherited] } ? self_class : self.class
21
+ self_class.orderable_configurations.any? { |_col, conf| conf[:inherited] } ? self_class : self.class
23
22
  end
24
23
  end
25
-
26
24
  end
27
25
  end
28
26
  end
29
- end
27
+ end
@@ -2,7 +2,6 @@ module Mongoid
2
2
  module Orderable
3
3
  module Generator
4
4
  module Listable
5
-
6
5
  def generate_listable_helpers(column_name)
7
6
  generate_list_helpers(column_name)
8
7
  generate_aliased_helpers(column_name)
@@ -34,7 +33,6 @@ module Mongoid
34
33
  alias_method "prev_#{column_name}_item", "previous_#{column_name}_item"
35
34
  end
36
35
  end
37
-
38
36
  end
39
37
  end
40
38
  end
@@ -2,7 +2,6 @@ module Mongoid
2
2
  module Orderable
3
3
  module Generator
4
4
  module Movable
5
-
6
5
  def generate_movable_helpers(column_name)
7
6
  generate_move_to_helpers(column_name)
8
7
  generate_insert_at_helpers(column_name)
@@ -13,16 +12,16 @@ module Mongoid
13
12
 
14
13
  def generate_move_to_helpers(column_name)
15
14
  generate_method("move_#{column_name}_to") do |target_position|
16
- move_column_to target_position, :column => column_name
15
+ move_column_to target_position, column: column_name
17
16
  end
18
17
 
19
18
  generate_method("move_#{column_name}_to!") do |target_position|
20
- move_column_to target_position, :column => column_name
19
+ move_column_to target_position, column: column_name
21
20
  save
22
21
  end
23
22
 
24
23
  generate_method("move_#{column_name}_to=") do |target_position|
25
- move_column_to target_position, :column => column_name
24
+ move_column_to target_position, column: column_name
26
25
  end
27
26
  end
28
27
 
@@ -37,26 +36,25 @@ module Mongoid
37
36
  def generate_shorthand_helpers(column_name)
38
37
  [:top, :bottom].each do |symbol|
39
38
  generate_method "move_#{column_name}_to_#{symbol}" do
40
- move_to symbol, :column => column_name
39
+ move_to symbol, column: column_name
41
40
  end
42
41
 
43
42
  generate_method "move_#{column_name}_to_#{symbol}!" do
44
- move_to! symbol, :column => column_name
43
+ move_to! symbol, column: column_name
45
44
  end
46
45
  end
47
46
 
48
47
  [:higher, :lower].each do |symbol|
49
48
  generate_method "move_#{column_name}_#{symbol}" do
50
- move_to symbol, :column => column_name
49
+ move_to symbol, column: column_name
51
50
  end
52
51
 
53
52
  generate_method "move_#{column_name}_#{symbol}!" do
54
- move_to! symbol, :column => column_name
53
+ move_to! symbol, column: column_name
55
54
  end
56
55
  end
57
56
  end
58
-
59
57
  end
60
58
  end
61
59
  end
62
- end
60
+ end
@@ -2,7 +2,6 @@ module Mongoid
2
2
  module Orderable
3
3
  module Generator
4
4
  module Position
5
-
6
5
  def generate_position_helpers(column_name)
7
6
  klass.class_eval <<-eos
8
7
  def orderable_position(column = nil)
@@ -19,7 +18,6 @@ module Mongoid
19
18
  send "#{column_name}=", value
20
19
  end
21
20
  end
22
-
23
21
  end
24
22
  end
25
23
  end
@@ -5,13 +5,13 @@ module Mongoid
5
5
  def generate_scope_helpers(column_name, order_scope)
6
6
  klass.class_eval do
7
7
  scope "orderable_#{column_name}_scope", case order_scope
8
- when Symbol then lambda { |document| where(order_scope => document.send(order_scope)) }
8
+ when Symbol then ->(document) { where(order_scope => document.send(order_scope)) }
9
9
  when Proc then order_scope
10
- else lambda { |document| where({}) }
10
+ else ->(_document) { where({}) }
11
11
  end
12
12
  end
13
13
  end
14
14
  end
15
15
  end
16
16
  end
17
- end
17
+ end
@@ -28,7 +28,6 @@ module Mongoid
28
28
  def generate_method(name, &block)
29
29
  klass.send(:define_method, name, &block)
30
30
  end
31
-
32
31
  end
33
32
  end
34
- end
33
+ end
@@ -1,50 +1,49 @@
1
1
  module Mongoid
2
2
  module Orderable
3
3
  module Helpers
4
-
5
4
  def orderable_keys
6
5
  Array orderable_inherited_class.orderable_configurations.try(:keys)
7
6
  end
8
7
 
9
8
  def default_orderable_column
10
- self.class.orderable_configurations.detect{ |c, conf| conf[:default] }.try(:first) || self.orderable_keys.first
9
+ self.class.orderable_configurations.detect { |_c, conf| conf[:default] }.try(:first) || orderable_keys.first
11
10
  end
12
11
 
13
12
  private
14
13
 
15
- def orderable_scoped(column = nil)
16
- column ||= default_orderable_column
14
+ def orderable_scoped(column = nil)
15
+ column ||= default_orderable_column
17
16
 
18
- if embedded?
19
- send(MongoidOrderable.metadata(self).inverse).send(MongoidOrderable.metadata(self).name).send("orderable_#{column}_scope", self)
20
- else
21
- self.orderable_inherited_class.send("orderable_#{column}_scope", self)
22
- end
17
+ if embedded?
18
+ _parent.send(MongoidOrderable.metadata(self).name).send("orderable_#{column}_scope", self)
19
+ else
20
+ orderable_inherited_class.send("orderable_#{column}_scope", self)
23
21
  end
22
+ end
24
23
 
25
- def orderable_scope_changed?(column)
26
- if Mongoid.respond_to?(:unit_of_work)
27
- Mongoid.unit_of_work do
28
- orderable_scope_changed_query(column)
29
- end
30
- else
24
+ def orderable_scope_changed?(column)
25
+ if Mongoid.respond_to?(:unit_of_work)
26
+ Mongoid.unit_of_work do
31
27
  orderable_scope_changed_query(column)
32
28
  end
29
+ else
30
+ orderable_scope_changed_query(column)
33
31
  end
32
+ end
34
33
 
35
- def orderable_scope_changed_query(column)
36
- !orderable_scoped(column).where(:_id => _id).exists?
37
- end
34
+ def orderable_scope_changed_query(column)
35
+ !orderable_scoped(column).where(_id: _id).exists?
36
+ end
38
37
 
39
- def bottom_orderable_position(column = nil)
40
- column ||= default_orderable_column
41
- @bottom_orderable_position = begin
42
- positions_list = orderable_scoped(column).distinct(orderable_column(column)).compact
43
- return orderable_base(column) if positions_list.empty?
44
- max = positions_list.map(&:to_i).max.to_i
45
- in_list?(column) ? max : max.next
46
- end
38
+ def bottom_orderable_position(column = nil)
39
+ column ||= default_orderable_column
40
+ @bottom_orderable_position = begin
41
+ positions_list = orderable_scoped(column).distinct(orderable_column(column)).compact
42
+ return orderable_base(column) if positions_list.empty?
43
+ max = positions_list.map(&:to_i).max.to_i
44
+ in_list?(column) ? max : max.next
47
45
  end
46
+ end
48
47
  end
49
48
  end
50
49
  end
@@ -1,49 +1,47 @@
1
1
  module Mongoid
2
2
  module Orderable
3
3
  module Listable
4
-
5
4
  ##
6
5
  # Returns items above the current document.
7
6
  # Items with a position lower than this document's position.
8
- def previous_items(column=nil)
9
- column = column || default_orderable_column
7
+ def previous_items(column = nil)
8
+ column ||= default_orderable_column
10
9
  orderable_scoped(column).where(orderable_column(column).lt => send(column))
11
10
  end
12
- alias_method :prev_items, :previous_items
11
+ alias prev_items previous_items
13
12
 
14
13
  ##
15
14
  # Returns items below the current document.
16
15
  # Items with a position greater than this document's position.
17
- def next_items(column=nil)
18
- column = column || default_orderable_column
16
+ def next_items(column = nil)
17
+ column ||= default_orderable_column
19
18
  orderable_scoped(column).where(orderable_column(column).gt => send(column))
20
19
  end
21
20
 
22
21
  # returns the previous item in the list
23
- def previous_item(column=nil)
24
- column = column || default_orderable_column
22
+ def previous_item(column = nil)
23
+ column ||= default_orderable_column
25
24
  orderable_scoped(column).where(orderable_column(column) => send(column) - 1).first
26
25
  end
27
- alias_method :prev_item, :previous_item
26
+ alias prev_item previous_item
28
27
 
29
28
  # returns the next item in the list
30
- def next_item(column=nil)
31
- column = column || default_orderable_column
29
+ def next_item(column = nil)
30
+ column ||= default_orderable_column
32
31
  orderable_scoped(column).where(orderable_column(column) => send(column) + 1).first
33
32
  end
34
33
 
35
- def first?(column=nil)
34
+ def first?(column = nil)
36
35
  in_list?(column) && orderable_position(column) == orderable_base(column)
37
36
  end
38
37
 
39
- def last?(column=nil)
38
+ def last?(column = nil)
40
39
  in_list?(column) && orderable_position(column) == bottom_orderable_position(column)
41
40
  end
42
41
 
43
- def in_list?(column=nil)
42
+ def in_list?(column = nil)
44
43
  !orderable_position(column).nil?
45
44
  end
46
-
47
45
  end
48
46
  end
49
47
  end
@@ -1,22 +1,21 @@
1
1
  module Mongoid
2
2
  module Orderable
3
3
  module Movable
4
-
5
- def move_to!(target_position, options={})
4
+ def move_to!(target_position, options = {})
6
5
  move_column_to target_position, options
7
6
  save
8
7
  end
9
- alias_method :insert_at!, :move_to!
8
+ alias insert_at! move_to!
10
9
 
11
- def move_to(target_position, options={})
10
+ def move_to(target_position, options = {})
12
11
  move_column_to target_position, options
13
12
  end
14
- alias_method :insert_at, :move_to
13
+ alias insert_at move_to
15
14
 
16
- def move_to=(target_position, options={})
15
+ def move_to=(target_position, options = {})
17
16
  move_column_to target_position, options
18
17
  end
19
- alias_method :insert_at=, :move_to=
18
+ alias insert_at= move_to=
20
19
 
21
20
  [:top, :bottom].each do |symbol|
22
21
  class_eval <<-eos
@@ -52,7 +51,6 @@ module Mongoid
52
51
  column = options[:column] || default_orderable_column
53
52
  @move_all = move_all.merge(column => position)
54
53
  end
55
-
56
54
  end
57
55
  end
58
56
  end
@@ -5,7 +5,7 @@ module Mongoid
5
5
 
6
6
  attr_reader :klass, :configuration
7
7
 
8
- def initialize klass, configuration
8
+ def initialize(klass, configuration)
9
9
  @klass = klass
10
10
  @configuration = configuration
11
11
  end
@@ -18,7 +18,7 @@ module Mongoid
18
18
  add_callbacks
19
19
  end
20
20
 
21
- def self.setup(klass, configuration={})
21
+ def self.setup(klass, configuration = {})
22
22
  new(klass, configuration).setup
23
23
  end
24
24
 
@@ -31,11 +31,7 @@ module Mongoid
31
31
  def add_db_index
32
32
  spec = [[configuration[:column], 1]]
33
33
  spec.unshift([configuration[:scope], 1]) if configuration[:scope].is_a?(Symbol)
34
- if ::Mongoid::Compatibility::Version.mongoid2?
35
- klass.index(spec)
36
- else
37
- klass.index(Hash[spec])
38
- end
34
+ klass.index(Hash[spec])
39
35
  end
40
36
 
41
37
  def save_configuration
@@ -11,12 +11,10 @@ module Mongoid::Orderable
11
11
  end
12
12
 
13
13
  module ClassMethods
14
-
15
- def orderable options = {}
14
+ def orderable(options = {})
16
15
  configuration = Mongoid::Orderable::Configuration.build(self, options)
17
16
 
18
17
  Mongoid::Orderable::OrderableClass.setup(self, configuration)
19
18
  end
20
-
21
19
  end
22
20
  end
@@ -1,3 +1,3 @@
1
1
  module MongoidOrderable
2
- VERSION = '5.0.0'
2
+ VERSION = '5.1.0'.freeze
3
3
  end
@@ -7,20 +7,20 @@ require 'mongoid'
7
7
  require 'mongoid/compatibility'
8
8
 
9
9
  module MongoidOrderable
10
- if ::Mongoid::Compatibility::Version.mongoid2? || ::Mongoid::Compatibility::Version.mongoid3?
11
- def self.inc instance, attribute, value
10
+ if ::Mongoid::Compatibility::Version.mongoid3?
11
+ def self.inc(instance, attribute, value)
12
12
  instance.inc attribute, value
13
13
  end
14
14
 
15
- def self.metadata instance
15
+ def self.metadata(instance)
16
16
  instance.metadata
17
17
  end
18
18
  else
19
- def self.inc instance, attribute, value
19
+ def self.inc(instance, attribute, value)
20
20
  instance.inc(attribute => value)
21
21
  end
22
22
 
23
- def self.metadata instance
23
+ def self.metadata(instance)
24
24
  instance.relation_metadata
25
25
  end
26
26
  end
@@ -28,13 +28,7 @@ end
28
28
 
29
29
  require 'mongoid_orderable/version'
30
30
 
31
- if ::Mongoid::Compatibility::Version.mongoid2?
32
- require 'mongoid_orderable/mongoid/contexts/mongo'
33
- require 'mongoid_orderable/mongoid/contexts/enumerable'
34
- require 'mongoid_orderable/mongoid/criteria'
35
- else
36
- require 'mongoid_orderable/mongoid/contextual/memory'
37
- end
31
+ require 'mongoid_orderable/mongoid/contextual/memory'
38
32
 
39
33
  require 'mongoid/orderable'
40
34
  require 'mongoid/orderable/errors'
@@ -1,26 +1,26 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "mongoid_orderable/version"
2
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
3
+ require 'mongoid_orderable/version'
4
4
 
5
5
  Gem::Specification.new do |s|
6
- s.name = "mongoid_orderable"
6
+ s.name = 'mongoid_orderable'
7
7
  s.version = MongoidOrderable::VERSION
8
- s.authors = ["pyromaniac"]
9
- s.email = ["kinwizard@gmail.com"]
10
- s.homepage = ""
11
- s.summary = %q{Acts as list mongoid implementation}
12
- s.description = %q{Gem allows mongoid model behave as orderable list}
8
+ s.authors = ['pyromaniac']
9
+ s.email = ['kinwizard@gmail.com']
10
+ s.homepage = ''
11
+ s.summary = 'Acts as list mongoid implementation'
12
+ s.description = 'Gem allows mongoid model behave as orderable list'
13
13
 
14
- s.rubyforge_project = "mongoid_orderable"
14
+ s.rubyforge_project = 'mongoid_orderable'
15
15
 
16
16
  s.files = `git ls-files`.split("\n")
17
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
- s.require_paths = ["lib"]
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
19
+ s.require_paths = ['lib']
20
20
 
21
21
  # specify any dependencies here; for example:
22
- s.add_development_dependency "rake"
23
- s.add_development_dependency "rspec"
24
- s.add_runtime_dependency "mongoid"
25
- s.add_runtime_dependency "mongoid-compatibility"
22
+ s.add_development_dependency 'rake'
23
+ s.add_development_dependency 'rspec'
24
+ s.add_runtime_dependency 'mongoid', '>= 3.0.0'
25
+ s.add_runtime_dependency 'mongoid-compatibility'
26
26
  end