flexible_feeds 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NTc4OTkwOWJiOTc3N2RlMTMwNGMyOWY0Y2YwN2U2N2Q5ZWQ2MGI1Yw==
4
+ Yzc0YmRkYzEwZTVjMjk2ZTMyN2EwMjcyNTBkZGZiYzM3M2UzNzZlOQ==
5
5
  data.tar.gz: !binary |-
6
- ZDI5NTYxYWM3NGY4MGVjMzljOWNmZDJjNzdlOGExY2M5MzUyOWRjMg==
6
+ OWZiZDM2ZjdiMmE1NTFmZGRhYmExMjIzOGVjMTIzMmY4ODBiZmQ2MA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OGUzNmQzZDQ5Mjk1NTNjNDgzMzlkM2JiMjI0Y2M5NzZhMGRlYWNkOGE3MWIx
10
- MGU4YTZlOTBhNzA4NGUxODQ2MDcwM2ZkMWE2NjUxNWFkNjFhNDk1ZmFmZjNl
11
- OGNjNmQ2MjJhYjQ1MTkyN2ZhNmRlM2I0ZjdiYTc0NWMyMGQ4ZTg=
9
+ YjZjYTQwN2Y2ZGM5MjNmMTY0M2E3MDFkNzA0NTRhM2VmNWVlYTMyNWRlZjFm
10
+ ZTc1MzVhOTk4YTA4OGE2OTVmNDk2NGM0M2QzMGU0YzdkYWVlMjU1YzliZDEy
11
+ MGU5ZGZiNzM0NmUyZWJmY2M0NTNmODNjZmE2NzNiMmZkZjRiZmQ=
12
12
  data.tar.gz: !binary |-
13
- MWYxMjU1NGRmMWY1MDgzYmEzYjgyMWFkMjMxN2IyZGVmODJkYmY1ZDVmNWMy
14
- ZjRmMzI4NWE4MTM5OGEwZjc1M2Q0NzczOGExODBhYTYxNmZlNDAxNmY4OTYz
15
- NWYwNDFiYjMxOGNmZDE3MWRhYmZjNzI3Y2U4ZmFlYTNkYjBjOGI=
13
+ NTBmYmQ4OThiMWViNTA1MWM4NzdiOGM0ZjI5YzFlNTA5ZTQ1MGNiMzVlOTJi
14
+ OTAzOWI3MjQ0NjRjMjljMGViZTAyODZlMDA0ODAxMGI5YTkyNDgwZjEwODcz
15
+ Y2JlY2FiOTljYzdlNDU3NTdlZjZjNzhjZjQ5NTM0YjNmMWEwODg=
@@ -1,10 +1,12 @@
1
1
  module FlexibleFeeds
2
2
  class Event < ActiveRecord::Base
3
- belongs_to :ancestor, polymorphic: true
4
- belongs_to :eventable, polymorphic: true
5
- belongs_to :parent, polymorphic: true
3
+ belongs_to :ancestor, class_name: "FlexibleFeeds::Event"
6
4
  belongs_to :creator, polymorphic: true
5
+ belongs_to :eventable, polymorphic: true
6
+ belongs_to :parent, class_name: "FlexibleFeeds::Event"
7
7
 
8
+ has_many :children, class_name: "FlexibleFeeds::Event",
9
+ foreign_key: :parent_id
8
10
  has_many :event_joins, dependent: :destroy
9
11
  has_many :feeds, through: :event_joins
10
12
  has_many :votes
@@ -41,6 +43,18 @@ module FlexibleFeeds
41
43
  save
42
44
  end
43
45
 
46
+ def increment_parent_counter
47
+ ancestors.each do |this_ancestor|
48
+ this_ancestor.increment(:children_count)
49
+ end
50
+ end
51
+
52
+ def decrement_parent_counter
53
+ ancestors.each do |this_ancestor|
54
+ this_ancestor.decrement(:children_count, children_count + 1)
55
+ end
56
+ end
57
+
44
58
  private
45
59
  def calculate_controversy(pos, neg)
46
60
  return 0 if pos == 0 || neg == 0
@@ -52,5 +66,15 @@ module FlexibleFeeds
52
66
  def calculate_popularity(pos, n)
53
67
  PopularityCalculator.new(pos, n).get_popularity
54
68
  end
69
+
70
+ def ancestors
71
+ ancestors = []
72
+ next_parent = parent
73
+ until next_parent.nil? do
74
+ ancestors.push next_parent
75
+ next_parent = next_parent.parent
76
+ end
77
+ ancestors
78
+ end
55
79
  end
56
80
  end
@@ -7,9 +7,7 @@ class CreateFlexibleFeedsEvents < ActiveRecord::Migration
7
7
  t.string :creator_type
8
8
 
9
9
  t.integer :parent_id
10
- t.string :parent_type
11
10
  t.integer :ancestor_id
12
- t.string :ancestor_type
13
11
  t.integer :children_count, null: false, default: 0
14
12
 
15
13
  t.integer :votes_sum, null: false, default: 0
@@ -27,17 +25,13 @@ class CreateFlexibleFeedsEvents < ActiveRecord::Migration
27
25
  add_index :flexible_feeds_events, :controversy
28
26
  add_index :flexible_feeds_events, :popularity
29
27
  add_index :flexible_feeds_events, :children_count
28
+ add_index :flexible_feeds_events, :ancestor_id
29
+ add_index :flexible_feeds_events, :parent_id
30
30
  add_index :flexible_feeds_events,
31
31
  [:eventable_id, :eventable_type],
32
32
  name: "flexible_feeds_events_on_eventable"
33
33
  add_index :flexible_feeds_events,
34
34
  [:creator_id, :creator_type],
35
35
  name: "flexible_feeds_events_on_creator"
36
- add_index :flexible_feeds_events,
37
- [:parent_id, :parent_type],
38
- name: "flexible_feeds_events_on_parent"
39
- add_index :flexible_feeds_events,
40
- [:ancestor_id, :ancestor_type],
41
- name: "flexible_feeds_events_on_ancestor"
42
36
  end
43
37
  end
@@ -9,6 +9,8 @@ module FlexibleFeeds
9
9
  true
10
10
  end
11
11
 
12
+ after_destroy :decrement_parent_counter
13
+
12
14
  send :include, InstanceMethods
13
15
  end
14
16
  end
@@ -27,9 +29,10 @@ module FlexibleFeeds
27
29
  end
28
30
 
29
31
  def child_of(parent)
30
- if can_accept_parent?(parent)
31
- ancestor = parent.try(:ancestor) || parent.event
32
- event.update_attributes(parent: parent.event, ancestor: ancestor)
32
+ if can_accept_parent?(parent.eventable)
33
+ ancestor = parent.try(:ancestor) || parent
34
+ event.update_attributes(parent: parent, ancestor: ancestor)
35
+ event.increment_parent_counter
33
36
  end
34
37
  end
35
38
 
@@ -47,6 +50,10 @@ module FlexibleFeeds
47
50
  def initialize_event
48
51
  event || create_event
49
52
  end
53
+
54
+ def decrement_parent_counter
55
+ event.decrement_parent_counter
56
+ end
50
57
  end
51
58
  end
52
59
  end
@@ -32,9 +32,10 @@ module FlexibleFeeds
32
32
  end
33
33
 
34
34
  def parent_of(child)
35
- if can_accept_child?(child)
35
+ if can_accept_child?(child.eventable)
36
36
  ancestor = self.try(:ancestor) || self.event
37
- child.event.update_attributes(parent: self.event, ancestor: ancestor)
37
+ child.update_attributes(parent: self.event, ancestor: ancestor)
38
+ child.increment_parent_counter
38
39
  end
39
40
  end
40
41
 
@@ -14,7 +14,9 @@ module FlexibleFeeds
14
14
 
15
15
  def define_association(join_table, association, singular_association)
16
16
  define_method association do
17
- send(join_table).collect { |join| join.send(singular_association) }
17
+ send(join_table).includes(singular_association).collect do |join|
18
+ join.send(singular_association)
19
+ end
18
20
  end
19
21
  end
20
22
 
@@ -1,3 +1,3 @@
1
1
  module FlexibleFeeds
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexible_feeds
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - timothycommoner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-06 00:00:00.000000000 Z
11
+ date: 2014-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails