Class: PostHistory

Inherits:
ApplicationRecord show all
Includes:
EditsValidations, PostRelated
Defined in:
app/models/post_history.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EditsValidations

#max_edit_comment_length

Methods inherited from ApplicationRecord

#attributes_print, fuzzy_search, match_search, #match_search, sanitize_for_search, sanitize_name, sanitize_sql_in, useful_err_msg, with_lax_group_rules

Class Method Details

.method_missing(name, *args, **opts) ⇒ Object



35
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
67
68
69
70
71
72
73
74
75
# File 'app/models/post_history.rb', line 35

def self.method_missing(name, *args, **opts)
  unless args.length >= 2
    raise NoMethodError
  end

  object, user = args
  fields = [:before, :after, :comment, :before_title, :after_title, :before_tags, :after_tags, :hidden]
  values = fields.to_h { |f| [f, nil] }.merge(opts)

  history_type_name = name.to_s
  history_type = PostHistoryType.find_by(name: history_type_name)
  if history_type.nil?
    super
    return
  end

  params = { post_history_type: history_type, user: user, post: object, community_id: object.community_id }
  { before: :before_state, after: :after_state, comment: :comment, before_title: :before_title,
    after_title: :after_title, hidden: :hidden }.each do |arg, attr|
    next if values[arg].nil?

    params = params.merge(attr => values[arg])
  end

  history = PostHistory.create params

  post_history_tags = { before_tags: 'before', after_tags: 'after' }.to_h do |arg, rel|
    if values[arg].nil?
      [arg, nil]
    else
      [arg, values[arg].map { |t| { post_history_id: history.id, tag_id: t.id, relationship: rel } }]
    end
  end.values.compact.flatten

  # do not create post history tags if post history validations failed
  unless history.errors.any?
    history.post_history_tags = PostHistoryTag.create(post_history_tags)
  end

  history
end

.redact(post, user) ⇒ Object

Hides all previous history

Parameters:



27
28
29
30
31
32
33
# File 'app/models/post_history.rb', line 27

def self.redact(post, user)
  where(post: post).update_all(hidden: true)
  history_hidden(post, user, after: post.body_markdown,
                                  after_title: post.title,
                                  after_tags: post.tags,
                                  comment: 'Detailed history before this event is hidden because of a redaction.')
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'app/models/post_history.rb', line 77

def self.respond_to_missing?(method_name, include_private = false)
  PostHistoryType.exists?(name: method_name.to_s) || super
end

Instance Method Details

#after_tagsObject



14
15
16
# File 'app/models/post_history.rb', line 14

def after_tags
  tags.where(post_history_tags: { relationship: 'after' })
end

#allowed_to_see_details?(user) ⇒ Boolean

Returns whether the given user is allowed to see the details of this history item.

Parameters:

Returns:

  • (Boolean)

    whether the given user is allowed to see the details of this history item



20
21
22
# File 'app/models/post_history.rb', line 20

def allowed_to_see_details?(user)
  !hidden || user&.is_admin || user_id == user&.id || post.user_id == user&.id
end

#before_tagsObject



10
11
12
# File 'app/models/post_history.rb', line 10

def before_tags
  tags.where(post_history_tags: { relationship: 'before' })
end