rails_admin_comments 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 92d36ad9f7b2fe74672829bd9e8ece9f6e857886
4
- data.tar.gz: 1ddf10c71da28a28c6e21b1163a2146b84e6629e
3
+ metadata.gz: 94998642dc3466c7483b166998e21c4a297ed5af
4
+ data.tar.gz: b1ec593416764e283ef3f631088e645da13a9849
5
5
  SHA512:
6
- metadata.gz: 44100a7f7b7aa06c0b66c2842cd8831f36258447576c52136c337bb0f0f2e37555a7c3e4730a30b22f013b4adf5c39cdecc17519cca5f4cbb526a463c86ee83d
7
- data.tar.gz: cbe891563d599013255d0ef4a77038df53c48af4c38a85171e58887c69ba4de354f3800a2b2fe81863e2dda4e6a950deae5b40acdbf7fde9bff1b32a63b5f863
6
+ metadata.gz: 7241a5c99a57620fed57585af142c05d2ef615ea16931c5a21e5aca888a849d4f0ed223a6da177871b1802e74cd0f1c44293890da63d41eb5cb415fcfa465ad8
7
+ data.tar.gz: dd14b0b239baf29ea56f459f06a00dd40ba28248f952a55359be6fe0776f1ca846d48342f0227c313a4a5cd58ce7ae01d869e2c0452da83a249afb1f4a135879
data/README.md CHANGED
@@ -25,7 +25,8 @@ Or install it yourself as:
25
25
  Add in model what you need to be commentable
26
26
 
27
27
  ```ruby
28
- include RailsAdminComments::Commentable
28
+ include RailsAdminComments::Commentable # comments for objects
29
+ include RailsAdminComments::ModelCommentable # comments for model (from v.0.2.0)
29
30
  ```
30
31
 
31
32
  And add action available for this model_name
@@ -38,9 +39,18 @@ And add action available for this model_name
38
39
  ].include? bindings[:abstract_model].model_name
39
40
  end
40
41
  end
42
+
43
+ # from v.0.2.0
44
+ model_comments do
45
+ visible do
46
+ [
47
+ "SomeModel"
48
+ ].include? bindings[:abstract_model].model_name
49
+ end
50
+ end
41
51
  ```
42
52
 
43
- Also you need to add method name_for_rails_admin_comments for user model_name
53
+ Also you need to add method name_for_rails_admin_comments for user model_name
44
54
 
45
55
  ```ruby
46
56
  def name_for_rails_admin_comments
@@ -4,3 +4,9 @@ $(document).delegate ".comment_edit_form_link", 'click', (e)->
4
4
  target = $(link.attr('href'))
5
5
  $('.comment_edit_form').addClass('hidden').closest('.comment_block').find(".content_block").removeClass('hidden')
6
6
  target.removeClass('hidden').closest('.comment_block').find(".content_block").addClass('hidden')
7
+
8
+ $(document).delegate ".comment_delete_form_link", 'click', (e)->
9
+ e.preventDefault()
10
+ link = $(e.currentTarget)
11
+ target = $(link.attr('href'))
12
+ target.find('form').prepend("<input type='hidden' name='_method' value='delete'></input>").submit()
@@ -19,6 +19,9 @@
19
19
  margin-left: 10px
20
20
  text-decoration: none
21
21
  border-bottom: 1px dotted
22
+ .comment_delete_form_link
23
+ margin-left: 0px
24
+
22
25
 
23
26
  textarea
24
27
  width: 70%
@@ -0,0 +1,12 @@
1
+ module RailsAdminComments
2
+ module ModelCommentable
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def rails_admin_model_comments
7
+ RailsAdminComments::ModelComment.where(model_name: self)
8
+ end
9
+ end
10
+
11
+ end
12
+ end
@@ -9,12 +9,12 @@ module RailsAdminComments
9
9
  class Comment
10
10
  #binding.pry
11
11
  if RailsAdminComments.mongoid?
12
- include RailsAdminComments::Mongoid
12
+ include RailsAdminComments::Comments::Mongoid
13
13
  end
14
14
 
15
15
  if RailsAdminComments.active_record?
16
16
  self.table_name = "rails_admin_comments"
17
17
  end
18
-
18
+
19
19
  end
20
20
  end
@@ -0,0 +1,20 @@
1
+ if RailsAdminComments.active_record?
2
+ module RailsAdminComments
3
+ class ModelComment < ActiveRecord::Base
4
+ end
5
+ end
6
+ end
7
+
8
+ module RailsAdminComments
9
+ class ModelComment
10
+ #binding.pry
11
+ if RailsAdminComments.mongoid?
12
+ include RailsAdminComments::ModelComments::Mongoid
13
+ end
14
+
15
+ if RailsAdminComments.active_record?
16
+ self.table_name = "rails_admin_model_comments"
17
+ end
18
+
19
+ end
20
+ end
@@ -1,3 +1,6 @@
1
+ - available_users = User.admins.to_a + User.managers.to_a
2
+ - available_users.uniq!
3
+
1
4
  #rails_admin_comments_wrapper
2
5
  = stylesheet_link_tag 'rails_admin/rails_admin_comments'
3
6
  = javascript_include_tag 'rails_admin/rails_admin_comments'
@@ -16,7 +19,10 @@
16
19
  = link_to creator.name_for_rails_admin_comments, show_path(model_name: model_name, id: creator._id), class: "creator_link"
17
20
  span.created_at= Russian::strftime c.c_at, "%d.%m.%Y в %H:%M:%S"
18
21
  span.updated_at= " (изм. #{Russian::strftime c.u_at, "%d.%m.%Y в %H:%M:%S"})" if Russian::strftime c.u_at
19
- = link_to 'изменить', "##{edit_form_id}", class: 'comment_edit_form_link' if creator == current_user
22
+ - if creator == current_user or (current_user.respond_to?(:admin?) and current_user.admin?)
23
+ = link_to 'изменить', "##{edit_form_id}", class: 'comment_edit_form_link'
24
+ = " | "
25
+ = link_to 'удалить', "##{edit_form_id}", class: 'comment_delete_form_link'
20
26
  p.content_block= c.content
21
27
  p
22
28
  .comment_edit_form.hidden{id="#{edit_form_id}"}
@@ -27,6 +33,8 @@
27
33
  - @comment = @message = nil
28
34
 
29
35
  = form_for c, url: url, method: :post do |f|
36
+ - if current_user.admin?
37
+ p= f.collection_check_boxes :visible_for_user_ids, available_users, :id, :name_for_rails_admin_comments
30
38
  p= f.hidden_field :id
31
39
  p= f.text_area :content
32
40
  p= f.submit
@@ -35,8 +43,12 @@
35
43
  hr
36
44
  - @comment ||= RailsAdminComments::Comment.new
37
45
  p
46
+ - unless @message.blank?
47
+ p== @message
38
48
  = form_for @comment, url: url, method: :post do |f|
39
- - unless @message.blank?
40
- p== @message
49
+ - if current_user.admin?
50
+ - if @comment.visible_for_users.blank?
51
+ - @comment.visible_for_users << available_users.select { |u| u.roles.include?("admin")}
52
+ p= f.collection_check_boxes :visible_for_user_ids, available_users, :id, :email
41
53
  p= f.text_area :content
42
54
  p= f.submit
@@ -0,0 +1,54 @@
1
+ - available_users = User.admins.to_a + User.managers.to_a
2
+ - available_users.uniq!
3
+
4
+ #rails_admin_comments_wrapper
5
+ = stylesheet_link_tag 'rails_admin/rails_admin_comments'
6
+ = javascript_include_tag 'rails_admin/rails_admin_comments'
7
+
8
+ #rails_admin_comments
9
+
10
+ - url = model_comments_path(model_name: @abstract_model)
11
+ .controls
12
+ - @comments.each do |c|
13
+ .comment_block
14
+ - edit_form_id = "comment_#{c.id}_edit"
15
+ p
16
+ - creator = c.creator
17
+ - if creator
18
+ - model_name = creator.class.to_param.gsub("::", "~").underscore
19
+ = link_to creator.name_for_rails_admin_comments, show_path(model_name: model_name, id: creator._id), class: "creator_link"
20
+ span.created_at= Russian::strftime c.c_at, "%d.%m.%Y в %H:%M:%S"
21
+ span.updated_at= " (изм. #{Russian::strftime c.u_at, "%d.%m.%Y в %H:%M:%S"})" if Russian::strftime c.u_at
22
+ - if creator == current_user or (current_user.respond_to?(:admin?) and current_user.admin?)
23
+ = link_to 'изменить', "##{edit_form_id}", class: 'comment_edit_form_link'
24
+ = " | "
25
+ = link_to 'удалить', "##{edit_form_id}", class: 'comment_delete_form_link'
26
+ p.content_block= c.content
27
+ p
28
+ .comment_edit_form.hidden{id="#{edit_form_id}"}
29
+ - if @comment and @comment._id == c._id
30
+ - c = @comment
31
+ - unless @message.blank?
32
+ p== @message
33
+ - @comment = @message = nil
34
+
35
+ = form_for c, url: url, method: :post do |f|
36
+ - if current_user.admin?
37
+ p= f.collection_check_boxes :visible_for_user_ids, available_users, :id, :name_for_rails_admin_comments
38
+ p= f.hidden_field :id
39
+ p= f.text_area :content
40
+ p= f.submit
41
+ br
42
+
43
+ hr
44
+ - @comment ||= RailsAdminComments::ModelComment.new
45
+ p
46
+ - unless @message.blank?
47
+ p== @message
48
+ = form_for @comment, url: url, method: :post do |f|
49
+ - if current_user.admin?
50
+ - if @comment.visible_for_users.blank?
51
+ - @comment.visible_for_users << available_users.select { |u| u.roles.include?("admin")}
52
+ p= f.collection_check_boxes :visible_for_user_ids, available_users, :id, :email
53
+ p= f.text_area :content
54
+ p= f.submit
@@ -2,7 +2,8 @@ if defined?(RailsAdmin)
2
2
  RailsAdmin.config do |config|
3
3
  config.excluded_models ||= []
4
4
  config.excluded_models << [
5
- 'RailsAdminComments::Comment'
5
+ 'RailsAdminComments::Comment',
6
+ 'RailsAdminComments::ModelComment'
6
7
  ]
7
8
  config.excluded_models.flatten!
8
9
  end
@@ -2,9 +2,17 @@ ru:
2
2
  admin:
3
3
  actions:
4
4
  comments:
5
- menu: "Коментарии"
6
- breadcrumb: "Коментарии"
5
+ menu: "Комментарии"
6
+ breadcrumb: "Комментарии"
7
7
  error: "Ошибка"
8
8
  error_no_data: "Нет данных"
9
9
  success: "Успешно"
10
- title: "Коментарии"
10
+ title: "Комментарии"
11
+
12
+ model_comments:
13
+ menu: "Комментарии"
14
+ breadcrumb: "Комментарии"
15
+ error: "Ошибка"
16
+ error_no_data: "Нет данных"
17
+ success: "Успешно"
18
+ title: "Комментарии"
@@ -24,11 +24,10 @@ module RailsAdmin
24
24
 
25
25
  register_instance_option :controller do
26
26
  Proc.new do |klass|
27
- @config = ::RailsAdminComments::Configuration.new @abstract_model
28
-
29
27
  if params['id'].present?
30
- @comments = @object.rails_admin_comments.by_date.enabled
28
+ @config = ::RailsAdminComments::Configuration.new @abstract_model
31
29
  if request.get?
30
+ @comments = @object.rails_admin_comments.by_date.enabled.for_user(current_user) || []
32
31
  render action: @action.template_name
33
32
 
34
33
  elsif request.post?
@@ -40,6 +39,7 @@ module RailsAdmin
40
39
  @comment = @object.rails_admin_comments.new if @comment.nil?
41
40
 
42
41
  @comment.content = params['rails_admin_comments_comment']['content']
42
+ @comment.visible_for_user_ids = params['rails_admin_comments_comment']['visible_for_user_ids']
43
43
 
44
44
  if @comment.save
45
45
  @comment = nil
@@ -72,7 +72,91 @@ module RailsAdmin
72
72
  end
73
73
 
74
74
  register_instance_option :http_methods do
75
- [:get, :post]
75
+ [:get, :post, :delete]
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+
83
+
84
+ module RailsAdmin
85
+ module Config
86
+ module Actions
87
+ class ModelComments < Comments
88
+ RailsAdmin::Config::Actions.register(self)
89
+
90
+ register_instance_option :collection? do
91
+ true
92
+ end
93
+
94
+ register_instance_option :member? do
95
+ false
96
+ end
97
+
98
+ register_instance_option :route_fragment do
99
+ 'model_comments'
100
+ end
101
+
102
+ register_instance_option :controller do
103
+ Proc.new do |klass|
104
+ @config = ::RailsAdminComments::Configuration.new @abstract_model
105
+ @model = @abstract_model.model
106
+ if request.get?
107
+ @comments = @model.rails_admin_model_comments.by_date.enabled.for_user(current_user) || []
108
+ render action: @action.template_name
109
+
110
+ elsif request.post?
111
+ begin
112
+ if params['rails_admin_comments_model_comment'].present?
113
+ if params['rails_admin_comments_model_comment']['id'].present?
114
+ @comment = @model.rails_admin_model_comments.find(params['rails_admin_comments_model_comment']['id'])
115
+ end
116
+ @comment ||= @model.rails_admin_model_comments.new
117
+
118
+ @comment.content = params['rails_admin_comments_model_comment']['content']
119
+ @comment.visible_for_user_ids = params['rails_admin_comments_model_comment']['visible_for_user_ids']
120
+
121
+ if @comment.save
122
+ @comment = nil
123
+ @message = "<strong class='success'>#{I18n.t('admin.actions.model_comments.success')}!</strong>"
124
+
125
+ else
126
+ @message = []
127
+ @message << "<strong class='error'>#{I18n.t('admin.actions.model_comments.error')}!</strong>"
128
+ @comment.errors.each do |e|
129
+ @message << "<p>#{e.message}</p>"
130
+ end
131
+ @message = @message.join
132
+ end
133
+ else
134
+ @message = "<strong class='error'>#{I18n.t('admin.actions.model_comments.error')}</strong>:<p>#{I18n.t('admin.actions.model_comments.error_no_data')}</p>"
135
+ end
136
+
137
+ rescue Exception => e
138
+ @message = "<strong class='error'>#{I18n.t('admin.actions.comments.error')}</strong>: #{e}"
139
+ end
140
+
141
+ render action: @action.template_name
142
+
143
+ elsif request.delete?
144
+ if params['rails_admin_comments_model_comment']['id'].present?
145
+ @comment = @model.rails_admin_model_comments.find(params['rails_admin_comments_model_comment']['id'])
146
+ end
147
+ if @comment and @comment.destroy
148
+ @comment = nil
149
+ @message = "Удалено"
150
+ else
151
+ @message = "Удаление не удалось"
152
+ end
153
+ render action: @action.template_name
154
+ end
155
+ end
156
+ end
157
+
158
+ register_instance_option :link_icon do
159
+ 'icon-comment'
76
160
  end
77
161
  end
78
162
  end
@@ -0,0 +1,29 @@
1
+ module RailsAdminComments
2
+ module Comments
3
+ module Mongoid
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ include ::Mongoid::Document
7
+ include ::Mongoid::Timestamps::Short
8
+ include ::Mongoid::Userstamp
9
+
10
+ belongs_to :rails_admin_commentable, polymorphic: true
11
+
12
+ store_in collection: "rails_admin_comments"
13
+
14
+ field :enabled, type: ::Mongoid::VERSION.to_i < 4 ? Boolean : ::Mongoid::Boolean, default: true
15
+ scope :enabled, -> { where(enabled: true) }
16
+
17
+ scope :by_date, -> { order([:c_at, :asc]) }
18
+
19
+ field :content
20
+ validates_presence_of :content
21
+
22
+ has_and_belongs_to_many :visible_for_users, class_name: "User", inverse_of: nil
23
+ scope :for_user, ->(user) {
24
+ any_of({visible_for_user_ids: user}, {visible_for_user_ids: nil})
25
+ }
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module RailsAdminComments
2
+ module ModelComments
3
+ module Mongoid
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ include ::Mongoid::Document
7
+ include ::Mongoid::Timestamps::Short
8
+ include ::Mongoid::Userstamp
9
+
10
+ field :model_name, type: String
11
+
12
+ store_in collection: "rails_admin_model_comments"
13
+
14
+ field :enabled, type: ::Mongoid::VERSION.to_i < 4 ? Boolean : ::Mongoid::Boolean, default: true
15
+ scope :enabled, -> { where(enabled: true) }
16
+
17
+ scope :by_date, -> { order([:c_at, :asc]) }
18
+
19
+ field :content
20
+ validates_presence_of :content
21
+
22
+ has_and_belongs_to_many :visible_for_users, class_name: "User", inverse_of: nil
23
+ scope :for_user, ->(user) {
24
+ any_of({visible_for_user_ids: user}, {visible_for_user_ids: nil})
25
+ }
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsAdminComments
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -4,8 +4,15 @@ require "rails_admin_comments/version"
4
4
  require 'mongoid'
5
5
  require 'mongoid_userstamp'
6
6
 
7
+ require "rails_admin_comments/engine"
8
+
9
+ require 'rails_admin_comments/configuration'
10
+
7
11
  require 'rails_admin/config/actions'
8
12
  require 'rails_admin/config/model'
13
+ require 'rails_admin_comments/action'
14
+ # require 'rails_admin_comments/model'
15
+ require 'rails_admin_comments/helper'
9
16
 
10
17
  module RailsAdminComments
11
18
  class << self
@@ -27,14 +34,11 @@ module RailsAdminComments
27
34
  end
28
35
  end
29
36
 
30
- autoload :Mongoid, "rails_admin_comments/mongoid"
37
+ module Comments
38
+ autoload :Mongoid, "rails_admin_comments/models/mongoid/comments"
39
+ end
40
+ module ModelComments
41
+ autoload :Mongoid, "rails_admin_comments/models/mongoid/model_comments"
42
+ end
31
43
  # autoload :RailsAdminConfig, "rails_admin_comments/rails_admin_config"
32
44
  end
33
-
34
-
35
- require "rails_admin_comments/engine"
36
-
37
- require 'rails_admin_comments/configuration'
38
- require 'rails_admin_comments/action'
39
- # require 'rails_admin_comments/model'
40
- require 'rails_admin_comments/helper'
@@ -29,8 +29,8 @@ Gem::Specification.new do |spec|
29
29
 
30
30
  spec.add_development_dependency "bundler", "~> 1.10"
31
31
  spec.add_development_dependency "rake", "~> 10.0"
32
-
33
- spec.add_dependency "rails"
32
+
33
+ spec.add_dependency "rails_admin"
34
34
  spec.add_dependency "mongoid", [">= 5.0", "< 6.0"]
35
35
  spec.add_dependency "glebtv_mongoid_userstamp"
36
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_admin_comments
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Kiseliev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-02 00:00:00.000000000 Z
11
+ date: 2016-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rails
42
+ name: rails_admin
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -104,8 +104,11 @@ files:
104
104
  - app/assets/javascripts/rails_admin/rails_admin_comments.coffee
105
105
  - app/assets/stylesheets/rails_admin/rails_admin_comments.sass
106
106
  - app/models/concerns/rails_admin_comments/commentable.rb
107
+ - app/models/concerns/rails_admin_comments/model_commentable.rb
107
108
  - app/models/rails_admin_comments/comment.rb
109
+ - app/models/rails_admin_comments/model_comment.rb
108
110
  - app/views/rails_admin/main/comments.html.slim
111
+ - app/views/rails_admin/main/model_comments.html.slim
109
112
  - bin/console
110
113
  - bin/setup
111
114
  - config/initializers/rails_admin.rb
@@ -115,7 +118,8 @@ files:
115
118
  - lib/rails_admin_comments/configuration.rb
116
119
  - lib/rails_admin_comments/engine.rb
117
120
  - lib/rails_admin_comments/helper.rb
118
- - lib/rails_admin_comments/mongoid.rb
121
+ - lib/rails_admin_comments/models/mongoid/comments.rb
122
+ - lib/rails_admin_comments/models/mongoid/model_comments.rb
119
123
  - lib/rails_admin_comments/version.rb
120
124
  - rails_admin_comments.gemspec
121
125
  - release.sh
@@ -1,24 +0,0 @@
1
- module RailsAdminComments
2
- module Mongoid
3
- extend ActiveSupport::Concern
4
- included do
5
- include ::Mongoid::Document
6
- include ::Mongoid::Timestamps::Short
7
- include ::Mongoid::Userstamp
8
-
9
- belongs_to :rails_admin_commentable, polymorphic: true
10
-
11
- store_in collection: "rails_admin_comments"
12
-
13
- field :enabled, type: ::Mongoid::VERSION.to_i < 4 ? Boolean : ::Mongoid::Boolean, default: true
14
- scope :enabled, -> { where(enabled: true) }
15
-
16
- scope :by_date, -> { order([:c_at, :asc]) }
17
-
18
- field :content
19
- validates_presence_of :content
20
-
21
- scope :for_user, ->(user) {}
22
- end
23
- end
24
- end