Class: PostActionTypeView

Inherits:
Object
  • Object
show all
Defined in:
lib/post_action_type_view.rb

Constant Summary collapse

ATTRIBUTE_NAMES =
%i[
  id
  name
  name_key
  description
  notify_type
  auto_action_type
  require_message
  applies_to
  position
  enabled
  score_type
]

Instance Method Summary collapse

Instance Method Details

#additional_message_typesObject



104
105
106
107
# File 'lib/post_action_type_view.rb', line 104

def additional_message_types
  return flag_settings.additional_message_types if overridden_by_plugin_or_skipped_db?
  flag_enum(all_flags.select { |flag| flag[:require_message] })
end

#all_flagsObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/post_action_type_view.rb', line 18

def all_flags
  @all_flags ||=
    Discourse
      .cache
      .fetch(PostActionType::POST_ACTION_TYPE_ALL_FLAGS_KEY) do
        Flag
          .unscoped
          .order(:position)
          .pluck(ATTRIBUTE_NAMES)
          .map { |attributes| ATTRIBUTE_NAMES.zip(attributes).to_h }
      end
end

#applies_toObject



123
124
125
126
127
128
# File 'lib/post_action_type_view.rb', line 123

def applies_to
  all_flags.reduce({}) do |acc, f|
    acc[f[:id]] = f[:applies_to]
    acc
  end
end

#auto_action_flag_typesObject



46
47
48
49
# File 'lib/post_action_type_view.rb', line 46

def auto_action_flag_types
  return flag_settings.auto_action_types if overridden_by_plugin_or_skipped_db?
  flag_enum(all_flags.select { |flag| flag[:auto_action_type] })
end

#descriptionsObject



116
117
118
119
120
121
# File 'lib/post_action_type_view.rb', line 116

def descriptions
  all_flags.reduce({}) do |acc, f|
    acc[f[:id]] = f[:description]
    acc
  end
end

#disabled_flag_typesObject



100
101
102
# File 'lib/post_action_type_view.rb', line 100

def disabled_flag_types
  flag_enum(all_flags.reject { |flag| flag[:enabled] })
end

#flag_settingsObject



31
32
33
# File 'lib/post_action_type_view.rb', line 31

def flag_settings
  @flag_settings ||= PostActionType.flag_settings
end

#flag_typesObject



72
73
74
75
# File 'lib/post_action_type_view.rb', line 72

def flag_types
  return flag_settings.flag_types if overridden_by_plugin_or_skipped_db?
  flag_enum(flags)
end

#flag_types_without_additional_messageObject



61
62
63
64
# File 'lib/post_action_type_view.rb', line 61

def flag_types_without_additional_message
  return flag_settings.without_additional_message_types if overridden_by_plugin_or_skipped_db?
  flag_enum(flags.reject { |flag| flag[:require_message] })
end

#flagsObject



66
67
68
69
70
# File 'lib/post_action_type_view.rb', line 66

def flags
  all_flags.reject do |flag|
    flag[:score_type] || flag[:id] == PostActionType::LIKE_POST_ACTION_ID
  end
end

#is_flag?(sym) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/post_action_type_view.rb', line 130

def is_flag?(sym)
  flag_types.valid?(sym)
end

#namesObject



109
110
111
112
113
114
# File 'lib/post_action_type_view.rb', line 109

def names
  all_flags.reduce({}) do |acc, f|
    acc[f[:id]] = f[:name]
    acc
  end
end

#notify_flag_type_idsObject

flags resulting in mod notifications



83
84
85
# File 'lib/post_action_type_view.rb', line 83

def notify_flag_type_ids
  notify_flag_types.values
end

#notify_flag_typesObject



87
88
89
90
# File 'lib/post_action_type_view.rb', line 87

def notify_flag_types
  return flag_settings.notify_types if overridden_by_plugin_or_skipped_db?
  flag_enum(all_flags.select { |flag| flag[:notify_type] })
end

#overridden_by_plugin_or_skipped_db?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/post_action_type_view.rb', line 42

def overridden_by_plugin_or_skipped_db?
  flag_settings.flag_types.present? || GlobalSetting.skip_db?
end

#public_type_idsObject



55
56
57
58
59
# File 'lib/post_action_type_view.rb', line 55

def public_type_ids
  Discourse
    .cache
    .fetch(PostActionType::POST_ACTION_TYPE_PUBLIC_TYPE_IDS_KEY) { public_types.values }
end

#public_typesObject



51
52
53
# File 'lib/post_action_type_view.rb', line 51

def public_types
  types.except(*flag_types.keys << :notify_user)
end

#score_typesObject



77
78
79
80
# File 'lib/post_action_type_view.rb', line 77

def score_types
  return flag_settings.flag_types if overridden_by_plugin_or_skipped_db?
  flag_enum(all_flags.filter { |flag| flag[:score_type] })
end

#topic_flag_typesObject



92
93
94
95
96
97
98
# File 'lib/post_action_type_view.rb', line 92

def topic_flag_types
  if overridden_by_plugin_or_skipped_db?
    flag_settings.topic_flag_types
  else
    flag_enum(all_flags.select { |flag| flag[:applies_to].include?("Topic") })
  end
end

#typesObject



35
36
37
38
39
40
# File 'lib/post_action_type_view.rb', line 35

def types
  if overridden_by_plugin_or_skipped_db?
    return Enum.new(like: PostActionType::LIKE_POST_ACTION_ID).merge!(flag_settings.flag_types)
  end
  Enum.new(like: PostActionType::LIKE_POST_ACTION_ID).merge(flag_types)
end