marks 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e86462ca450799da8d9cc00a1e189703a7e20ed
4
- data.tar.gz: 3c4bf861b0d599b2f78e1c590aa3edcab2492bea
3
+ metadata.gz: 0b15c7e6a7569ff12c6b88ef8b48060946b5af51
4
+ data.tar.gz: fcb939ebd6d81dbaaa20aa46e4650875933f1fc0
5
5
  SHA512:
6
- metadata.gz: 2b1604676f45f4152e59d2e38f432eec222eafed0d3cedce2911d57f49f9eb5c967a0b8bce8517efa8ca444bec4e0e9c0dde437648166e32edfe5c5f201b0ca9
7
- data.tar.gz: 4755d17d2d6c7537fff745b08b138ccd7377d285918739a18e58ee447f8b22e5b0da8995b299dd53eea4cf4755e2c909fc5444118a5b5cb76dcbb8a8e675ba9d
6
+ metadata.gz: 18f1e61375f3387e713d4f41bb5a7b3f35289a27bf54a48d73f5f0cc230e93f51f81b9894563b0e8e50154ec1424d67dddfd694cbd48afea9fdfcf3fe4801be1
7
+ data.tar.gz: df604dee365c1b15c40451626f8314a760a7a55d3851a0dddf7cba7d0d0f567e423f205377c73a1ca277cba95fb13f2a3bda252f872cddf70cc284c001bbb3e5
@@ -1,6 +1,7 @@
1
1
  require 'marks/engine'
2
2
  require 'marks/marker'
3
3
  require 'marks/markable'
4
+ require 'marks/relation_extension'
4
5
 
5
6
  module Marks
6
7
  end
@@ -11,3 +12,7 @@ begin
11
12
  rescue LoadError
12
13
  #do nothing
13
14
  end
15
+
16
+ ActiveSupport.on_load(:active_record) do
17
+ ActiveRecord::Relation.send(:include, Marks::RelationExtension)
18
+ end
@@ -6,6 +6,11 @@ module Marks
6
6
  def markable_with(*types)
7
7
  has_many :incoming_marks, as: :markable, dependent: :destroy, class_name: 'Marks::Mark'
8
8
 
9
+ types.each do |type|
10
+ # just for preload_marker's result cache, which is used by marked_by?
11
+ has_one :"#{type}_marked_flag"
12
+ end
13
+
9
14
  class_eval do
10
15
  define_method :'markers' do |class_name, mark|
11
16
  classified_mark = mark.to_s.classify
@@ -18,6 +23,12 @@ module Marks
18
23
  classified_mark = mark.to_s.classify
19
24
  raise ArgumentError unless types.map { |t| t.to_s.classify }.include?(classified_mark)
20
25
  return false unless marker
26
+
27
+ # if preload_marker is executed, use its result
28
+ if self.association(:"#{mark}_marked_flag").loaded?
29
+ return self.association(:"#{mark}_marked_flag").target
30
+ end
31
+
21
32
  klass = marker.class.table_name.classify.constantize
22
33
  klass.joins(:outgoing_marks).where('marks_marks.mark_type = ? AND markable_type = ? AND markable_id = ?', mark.to_s.classify, self.class.base_class.to_s, self).any?
23
34
  end
@@ -0,0 +1,39 @@
1
+ module Marks
2
+ module RelationExtension
3
+ attr_accessor :marker_value, :mark_type_values
4
+
5
+ def self.included(klass)
6
+ klass.class_eval do
7
+ alias_method_chain :exec_queries, :marks
8
+ end
9
+ end
10
+
11
+ def preload_marker(marker, *mark_types)
12
+ self.marker_value = marker
13
+ self.mark_type_values = mark_types
14
+ self
15
+ end
16
+
17
+ private
18
+
19
+ def exec_queries_with_marks
20
+ records = exec_queries_without_marks
21
+
22
+ if marker_value.present? && mark_type_values.present?
23
+ mark_type_values.each do |mark_type|
24
+ preload_marker_for(records, marker_value, mark_type)
25
+ end
26
+ end
27
+
28
+ records
29
+ end
30
+
31
+ def preload_marker_for(records, marker, mark_type)
32
+ markable_ids = Mark.where(markable: records, marker: marker, mark_type: mark_type.to_s.classify).pluck(:markable_id)
33
+
34
+ records.each do |record|
35
+ record.association(:"#{mark_type}_marked_flag").target = markable_ids.include?(record.id)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,3 +1,3 @@
1
1
  module Marks
2
- VERSION = '0.0.10'
2
+ VERSION = '0.0.11'
3
3
  end
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: marks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masahiro Ihara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-10 00:00:00.000000000 Z
11
+ date: 2014-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 3.2.9
19
+ version: 4.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 3.2.9
26
+ version: 4.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: sqlite3
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: pry
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: An engine that enable you to mark any ActiveRecord model as many as you
@@ -74,17 +74,18 @@ executables: []
74
74
  extensions: []
75
75
  extra_rdoc_files: []
76
76
  files:
77
+ - MIT-LICENSE
78
+ - README.md
79
+ - Rakefile
77
80
  - app/models/marks/mark.rb
78
81
  - db/migrate/20121116121923_create_marks_marks.rb
82
+ - lib/marks.rb
79
83
  - lib/marks/engine.rb
80
84
  - lib/marks/markable.rb
81
85
  - lib/marks/marker.rb
86
+ - lib/marks/relation_extension.rb
82
87
  - lib/marks/version.rb
83
- - lib/marks.rb
84
88
  - lib/tasks/marks_tasks.rake
85
- - MIT-LICENSE
86
- - Rakefile
87
- - README.md
88
89
  homepage: https://github.com/ihara2525/marks
89
90
  licenses: []
90
91
  metadata: {}
@@ -94,17 +95,17 @@ require_paths:
94
95
  - lib
95
96
  required_ruby_version: !ruby/object:Gem::Requirement
96
97
  requirements:
97
- - - '>='
98
+ - - ">="
98
99
  - !ruby/object:Gem::Version
99
100
  version: '0'
100
101
  required_rubygems_version: !ruby/object:Gem::Requirement
101
102
  requirements:
102
- - - '>='
103
+ - - ">="
103
104
  - !ruby/object:Gem::Version
104
105
  version: '0'
105
106
  requirements: []
106
107
  rubyforge_project:
107
- rubygems_version: 2.0.3
108
+ rubygems_version: 2.2.2
108
109
  signing_key:
109
110
  specification_version: 4
110
111
  summary: An engine that enable you to mark any ActiveRecord model as many as you like