Module: ActionStore::Mixin::ClassMethods

Defined in:
lib/action_store/mixin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defined_actionsObject (readonly)

Returns the value of attribute defined_actions.



26
27
28
# File 'lib/action_store/mixin.rb', line 26

def defined_actions
  @defined_actions
end

Instance Method Details

#action_store(action_type, name, class_name: nil, action_class_name: "Action", counter_cache: nil, user_counter_cache: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/action_store/mixin.rb', line 36

def action_store(action_type, name, class_name: nil, action_class_name: "Action", counter_cache: nil, user_counter_cache: nil)
  name = name.to_s

  class_name ||= name.classify
  target_klass = class_name.constantize
  action_klass = action_class_name.constantize
  action_type = action_type.to_s
  if counter_cache == true
    # @post.stars_count
    counter_cache = "#{action_type.pluralize}_count"
  end
  if user_counter_cache == true
    # @user.star_posts_count
    user_counter_cache = "#{action_type}_#{name.pluralize}_count"
  end

  @defined_actions ||= []
  action = {
    action_name: name,
    action_type: action_type,
    action_klass: action_klass,
    target_klass: target_klass,
    target_type: target_klass.name,
    counter_cache: counter_cache,
    user_counter_cache: user_counter_cache
  }
  @defined_actions << action

  define_relations(action_klass: action_klass, target_klass: target_klass,
                   action_type: action_type, action_name: name)
end

#create_action(action_type, opts) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/action_store/mixin.rb', line 80

def create_action(action_type, opts)
  opts[:action_type] = action_type
  opts = safe_action_opts(opts)
  return false if opts[:user_id].blank? || opts[:user_type].blank?
  return false if opts[:target_id].blank? || opts[:target_type].blank?

  defined_action = find_defined_action(opts[:action_type], opts[:target_type])
  return false if defined_action.nil?

  # create! for raise RecordNotUnique
  begin
    action = defined_action[:action_klass].find_or_create_by!(where_opts(opts))
    action.update(action_option: opts[:action_option]) if opts.key?(:action_option)
  rescue ActiveRecord::RecordNotUnique
    # update action_option on exist
    action = defined_action[:action_klass].where(where_opts(opts)).take
    action.update(action_option: opts[:action_option]) if opts.key?(:action_option)
  end

  reset_counter_cache(action, defined_action)
  true
end

#destroy_action(action_type, opts) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/action_store/mixin.rb', line 103

def destroy_action(action_type, opts)
  opts[:action_type] = action_type
  opts = safe_action_opts(opts)
  return false if opts[:user_id].blank? || opts[:user_type].blank?
  return false if opts[:target_id].blank? || opts[:target_type].blank?

  defined_action = find_defined_action(opts[:action_type], opts[:target_type])
  return false if defined_action.nil?

  action = defined_action[:action_klass].where(where_opts(opts)).first
  return true unless action

  action.destroy
  reset_counter_cache(action, defined_action)
  true
end

#find_action(action_type, opts) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/action_store/mixin.rb', line 68

def find_action(action_type, opts)
  opts[:action_type] = action_type
  opts = safe_action_opts(opts)
  return nil if opts[:user_id].blank? || opts[:user_type].blank?
  return nil if opts[:target_id].blank? || opts[:target_type].blank?

  defined_action = find_defined_action(opts[:action_type], opts[:target_type])
  return nil if defined_action.nil?

  defined_action[:action_klass].find_by(where_opts(opts))
end

#find_defined_action(action_type, target_type) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/action_store/mixin.rb', line 28

def find_defined_action(action_type, target_type)
  action_type = action_type.to_s
  name = target_type.to_s.singularize.underscore
  defined_actions.find do |a|
    a[:action_type] == action_type && (a[:action_name] == name || a[:target_type] == target_type)
  end
end

#reset_counter_cache(action, defined_action) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/action_store/mixin.rb', line 120

def reset_counter_cache(action, defined_action)
  return false if action.blank?

  if defined_action[:counter_cache] && action.target.present?
    target_count = defined_action[:action_klass].where(
      action_type: defined_action[:action_type],
      target_type: action.target_type,
      target_id: action.target_id
    ).count
    action.target.update_attribute(defined_action[:counter_cache], target_count)
  end

  if defined_action[:user_counter_cache] && action.user.present?
    user_count = defined_action[:action_klass].where(
      action_type: defined_action[:action_type],
      target_type: action.target_type,
      user_type: action.user_type,
      user_id: action.user_id
    ).count
    action.user.update_attribute(defined_action[:user_counter_cache], user_count)
  end
end