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, opts = {}) ⇒ 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
# File 'lib/action_store/mixin.rb', line 36

def action_store(action_type, name, opts = {})
  opts ||= {}
  klass_name = opts[:class_name] || name.to_s.classify
  target_klass = klass_name.constantize
  action_type = action_type.to_s
  if opts[:counter_cache] == true
    # @post.stars_count
    opts[:counter_cache] = "#{action_type.pluralize}_count"
  end
  if opts[:user_counter_cache] == true
    # @user.star_posts_count
    opts[:user_counter_cache] = "#{action_type}_#{name.to_s.pluralize}_count"
  end

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

  define_relations(action)
end

#create_action(action_type, opts) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/action_store/mixin.rb', line 76

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 = Action.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 = Action.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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/action_store/mixin.rb', line 99

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 = Action.where(where_opts(opts)).first
  return true if !action
  action.destroy
  reset_counter_cache(action, defined_action)
  true
end

#find_action(action_type, opts) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/action_store/mixin.rb', line 64

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?

  Action.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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/action_store/mixin.rb', line 115

def reset_counter_cache(action, defined_action)
  return false if action.blank?
  if defined_action[:counter_cache] && action.target.present?
    target_count = Action.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 = Action.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