Class: PostHistory

Inherits:
ApplicationRecord show all
Includes:
EditsValidations, PostRelated
Defined in:
/var/apps/qpixel/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



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
76
77
78
79
80
# File '/var/apps/qpixel/app/models/post_history.rb', line 40

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:

  • post (Post)

    post to redact history for

  • user (User)

    user that is redacting the history



32
33
34
35
36
37
38
# File '/var/apps/qpixel/app/models/post_history.rb', line 32

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)


82
83
84
# File '/var/apps/qpixel/app/models/post_history.rb', line 82

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

Instance Method Details

#after_tagsObject



18
19
20
# File '/var/apps/qpixel/app/models/post_history.rb', line 18

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

#allowed_to_see_details?(user) ⇒ Boolean

Checks whether a given user is allowed to see post history item details

Parameters:

  • user (User)

    user to check for

Returns:

  • (Boolean)

    check result



25
26
27
# File '/var/apps/qpixel/app/models/post_history.rb', line 25

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

#before_tagsObject



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

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