Module: PostTypesHelper

Defined in:
app/helpers/post_types_helper.rb

Instance Method Summary collapse

Instance Method Details

#post_type_badge(type) ⇒ ActiveSupport::SafeBuffer

Create a badge to display the specified post type.

Parameters:

Returns:

  • (ActiveSupport::SafeBuffer)


6
7
8
9
10
# File 'app/helpers/post_types_helper.rb', line 6

def post_type_badge(type)
  tag.span class: 'badge is-tag is-filled is-muted' do
    "#{tag.i(class: type.icon_name)} #{tag.span(type.name)}".html_safe
  end
end

#post_type_criteriaArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get a list of predicate post type attributes (i.e. is_* and has_* attributes).

Returns:

  • (Array<Symbol>)


16
17
18
# File 'app/helpers/post_types_helper.rb', line 16

def post_type_criteria
  PostType.new.attributes.keys.select { |k| k.start_with?('has_') || k.start_with?('is_') }.map(&:to_sym)
end

#post_type_ids(**opts) ⇒ Array<Integer>

Get a list of post type IDs matching specified criteria. Available criteria are based on predicate attributes on the post_types table (i.e. has_* and is_* attributes).

Examples:

Query for IDs of top-level post types which are freely editable and have reactions:

helpers.post_type_ids(is_top_level: true, is_freely_editable: true, has_reactions: true)
# => [12, 23, 49]

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :has_answers (Boolean)
  • :has_votes (Boolean)
  • :has_tags (Boolean)
  • :has_parent (Boolean)
  • :has_category (Boolean)
  • :has_license (Boolean)
  • :is_public_editable (Boolean)
  • :is_closeable (Boolean)
  • :is_top_level (Boolean)
  • :is_freely_editable (Boolean)
  • :has_reactions (Boolean)
  • :has_only_specific_reactions (Boolean)

Returns:

  • (Array<Integer>)


39
40
41
42
43
44
# File 'app/helpers/post_types_helper.rb', line 39

def post_type_ids(**opts)
  key = post_type_criteria.map { |a| opts[a] ? '1' : '0' }.join
  Rails.cache.fetch "network/post_types/post_type_ids/#{key}", include_community: false do
    PostType.where(**opts).select(:id).map(&:id)
  end
end