Class: ActiveAdmin::Comments::Views::Comments

Inherits:
Views::Panel show all
Defined in:
lib/active_admin/orm/active_record/comments/views/active_admin_comments.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Views::Panel

#add_child, #children?, #header_action

Instance Attribute Details

#resourceObject

Returns the value of attribute resource.



11
12
13
# File 'lib/active_admin/orm/active_record/comments/views/active_admin_comments.rb', line 11

def resource
  @resource
end

Instance Method Details

#build(resource) ⇒ Object



13
14
15
16
17
18
# File 'lib/active_admin/orm/active_record/comments/views/active_admin_comments.rb', line 13

def build(resource)
  @resource = resource
  @comments = ActiveAdmin::Comment.find_for_resource_in_namespace(resource, active_admin_namespace.name).includes(:author).page(params[:page])
  super(title, for: resource)
  build_comments
end

#build_comment(comment) ⇒ Object (protected)



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/active_admin/orm/active_record/comments/views/active_admin_comments.rb', line 38

def build_comment(comment)
  div for: comment do
    div class: 'active_admin_comment_meta' do
      h4 class: 'active_admin_comment_author' do
        comment.author ? auto_link(comment.author) : I18n.t('active_admin.comments.author_missing')
      end
      span pretty_format comment.created_at
      if authorized?(ActiveAdmin::Auth::DESTROY, comment)
        text_node link_to I18n.t('active_admin.comments.delete'), comments_url(comment.id), method: :delete, data: { confirm: I18n.t('active_admin.comments.delete_confirmation') }
      end
    end
    div class: 'active_admin_comment_body' do
      simple_format comment.body
    end
  end
end

#build_comment_formObject (protected)



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/active_admin/orm/active_record/comments/views/active_admin_comments.rb', line 75

def build_comment_form
  active_admin_form_for(ActiveAdmin::Comment.new, url: comment_form_url) do |f|
    f.inputs do
      f.input :resource_type, as: :hidden,  input_html: { value: ActiveAdmin::Comment.resource_type(parent.resource) }
      f.input :resource_id,   as: :hidden,  input_html: { value: parent.resource.id }
      f.input :body,          label: false, input_html: { size: '80x8' }
    end
    f.actions do
      f.action :submit, label: I18n.t('active_admin.comments.add')
    end
  end
end

#build_commentsObject (protected)



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_admin/orm/active_record/comments/views/active_admin_comments.rb', line 26

def build_comments
  if @comments.any?
    @comments.each(&method(:build_comment))
    div page_entries_info(@comments).html_safe, class: 'pagination_information'
  else
    build_empty_message
  end

  text_node paginate @comments
  build_comment_form
end

#build_empty_messageObject (protected)



55
56
57
# File 'lib/active_admin/orm/active_record/comments/views/active_admin_comments.rb', line 55

def build_empty_message
  span I18n.t('active_admin.comments.no_comments_yet'), class: 'empty'
end

#comment_form_urlObject (protected)



67
68
69
70
71
72
73
# File 'lib/active_admin/orm/active_record/comments/views/active_admin_comments.rb', line 67

def comment_form_url
  parts = []
  parts << active_admin_namespace.name unless active_admin_namespace.root?
  parts << active_admin_namespace.comments_registration_name.underscore.pluralize
  parts << 'path'
  send parts.join '_'
end

#comments_url(*args) ⇒ Object (protected)



59
60
61
62
63
64
65
# File 'lib/active_admin/orm/active_record/comments/views/active_admin_comments.rb', line 59

def comments_url(*args)
  parts = []
  parts << active_admin_namespace.name unless active_admin_namespace.root?
  parts << active_admin_namespace.comments_registration_name.underscore
  parts << 'path'
  send parts.join('_'), *args
end

#default_id_for_prefixObject (protected)



88
89
90
# File 'lib/active_admin/orm/active_record/comments/views/active_admin_comments.rb', line 88

def default_id_for_prefix
  'active_admin_comments_for'
end

#titleObject (protected)



22
23
24
# File 'lib/active_admin/orm/active_record/comments/views/active_admin_comments.rb', line 22

def title
  I18n.t 'active_admin.comments.title_content', count: @comments.total_count
end