hancock_cms_feedback 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.ruby-gemset +1 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +4 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +60 -0
  9. data/Rakefile +1 -0
  10. data/app/assets/javascripts/hancock/feedback.coffee +8 -0
  11. data/app/assets/javascripts/hancock/feedback/form.coffee +77 -0
  12. data/app/assets/javascripts/hancock/feedback/init.coffee +2 -0
  13. data/app/assets/stylesheets/hancock/feedback.sass +0 -0
  14. data/app/controllers/concerns/hancock/feedback/decorators/contacts.rb +84 -0
  15. data/app/controllers/hancock/feedback/contacts_controller.rb +7 -0
  16. data/app/mailers/hancock/feedback/contact_mailer.rb +17 -0
  17. data/app/models/concerns/hancock/feedback/decorators/contact_message.rb +44 -0
  18. data/app/models/hancock/feedback/contact_message.rb +17 -0
  19. data/app/views/hancock/feedback/contact_mailer/new_message_email.html.slim +15 -0
  20. data/app/views/hancock/feedback/contacts/_fields.html.slim +18 -0
  21. data/app/views/hancock/feedback/contacts/_fields_with_settings.html.slim +24 -0
  22. data/app/views/hancock/feedback/contacts/_form.html.slim +37 -0
  23. data/app/views/hancock/feedback/contacts/_form_with_wrapper.html.slim +2 -0
  24. data/app/views/hancock/feedback/contacts/_simple_captcha.html.slim +17 -0
  25. data/app/views/hancock/feedback/contacts/_success.html.slim +4 -0
  26. data/app/views/hancock/feedback/contacts/index.html.slim +8 -0
  27. data/app/views/hancock/feedback/contacts/new.html.slim +8 -0
  28. data/app/views/hancock/feedback/contacts/sent.html.slim +3 -0
  29. data/app/views/hancock/feedback/contacts/update_captcha.html.slim +21 -0
  30. data/bin/console +15 -0
  31. data/bin/setup +7 -0
  32. data/config/locales/hancock.feedback.ru.yml +56 -0
  33. data/config/locales/ru.simple_captcha.yml +3 -0
  34. data/config/locales/ru.simple_form.yml +9 -0
  35. data/hancock_cms_feedback.gemspec +39 -0
  36. data/lib/generators/hancock/feedback/config/config_generator.rb +13 -0
  37. data/lib/generators/hancock/feedback/config/templates/hancock_feedback.erb +19 -0
  38. data/lib/generators/hancock/feedback/controllers/decorators_generator.rb +24 -0
  39. data/lib/generators/hancock/feedback/controllers/templates/contacts.erb +145 -0
  40. data/lib/generators/hancock/feedback/controllers/transfers_generator.rb +39 -0
  41. data/lib/generators/hancock/feedback/migrations/migrations_generator.rb +18 -0
  42. data/lib/generators/hancock/feedback/migrations/templates/migration_contact_messages.erb +17 -0
  43. data/lib/hancock/feedback/admin.rb +6 -0
  44. data/lib/hancock/feedback/admin/contact_message.rb +40 -0
  45. data/lib/hancock/feedback/configuration.rb +51 -0
  46. data/lib/hancock/feedback/controllers/contacts.rb +154 -0
  47. data/lib/hancock/feedback/engine.rb +36 -0
  48. data/lib/hancock/feedback/models/active_record/contact_message.rb +13 -0
  49. data/lib/hancock/feedback/models/contact_message.rb +92 -0
  50. data/lib/hancock/feedback/models/mongoid/contact_message.rb +35 -0
  51. data/lib/hancock/feedback/routes.rb +42 -0
  52. data/lib/hancock/feedback/version.rb +5 -0
  53. data/lib/hancock_cms_feedback.rb +34 -0
  54. data/release.sh +6 -0
  55. metadata +187 -0
@@ -0,0 +1,39 @@
1
+ require 'rails/generators'
2
+
3
+ module Hancock::Feedback::Controllers
4
+ class ContactsGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('../templates', __FILE__)
6
+ argument :class_name_arg, type: :string, default: ""
7
+
8
+ desc 'Hancock::Feedback Contacts Controller generator'
9
+ def transfers
10
+ template 'contacts.erb', "app/controllers/#{file_name}.rb"
11
+ end
12
+
13
+ private
14
+ def class_name
15
+ class_name_arg.blank? ? "SetClassForContactsController" : class_name_arg
16
+ end
17
+
18
+ def capitalized_class_name
19
+ class_name.capitalize
20
+ end
21
+
22
+ def camelcased_class_name
23
+ class_name.camelcase
24
+ end
25
+
26
+ def file_name
27
+ underscored_class_name
28
+ end
29
+
30
+ def underscored_class_name
31
+ camelcased_class_name.underscore
32
+ end
33
+
34
+ def underscored_pluralized_class_name
35
+ underscored_class_name.pluralize
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,18 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/active_record'
3
+
4
+ module Hancock::Feedback
5
+ class MigrationsGenerator < Rails::Generators::Base
6
+ include ActiveRecord::Generators::Migration
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ desc 'Hancock Feedback migration generator'
10
+ def migrations
11
+ if Hancock::Feedback.active_record?
12
+ %w(contact_messages).each do |table_name|
13
+ migration_template "migration_#{table_name}.rb", "db/migrate/hancock_feedback_create_#{table_name}.rb"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ class HancockFeedbackCreateContactMessages < ActiveRecord::Migration
2
+ def change
3
+ create_table :hancock_feedback_contact_messages do |t|
4
+ t.string :name
5
+ t.string :email
6
+ t.string :phone
7
+ t.text :content
8
+ <!-- <% Hancock::Feedback.config.fields.each_pair do |name, type| %>
9
+ t.<%= type %> :<%= name %>
10
+ <% end %> -->
11
+ t.timestamps
12
+ end
13
+
14
+ add_index :hancock_feedback_contact_messages, :created_at
15
+ add_index :hancock_feedback_contact_messages, :updated_at
16
+ end
17
+ end
@@ -0,0 +1,6 @@
1
+ module Hancock::Feedback
2
+ module Admin
3
+ class << self
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,40 @@
1
+ module Hancock::Feedback
2
+ module Admin
3
+ module ContactMessage
4
+ def self.config(nav_label = nil, fields = {})
5
+ if nav_label.is_a?(Hash)
6
+ nav_label, fields = nav_label[:nav_label], nav_label
7
+ elsif nav_label.is_a?(Array)
8
+ nav_label, fields = nil, nav_label
9
+ end
10
+
11
+ Proc.new {
12
+ navigation_label(!nav_label.blank? ? nav_label : I18n.t('hancock.feedback'))
13
+
14
+ field :c_at do
15
+ read_only true
16
+ end
17
+ field :name do
18
+ searchable true
19
+ end
20
+ field :content, :text do
21
+ searchable true
22
+ end
23
+ field :email do
24
+ searchable true
25
+ end
26
+ field :phone do
27
+ searchable true
28
+ end
29
+
30
+ Hancock::RailsAdminGroupPatch::hancock_cms_group(self, fields)
31
+
32
+ if block_given?
33
+ yield self
34
+ end
35
+
36
+ }
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,51 @@
1
+ module Hancock::Feedback
2
+ include Hancock::PluginConfiguration
3
+
4
+ def self.config_class
5
+ Configuration
6
+ end
7
+
8
+ class Configuration
9
+ attr_accessor :captcha
10
+ attr_accessor :message_required
11
+
12
+ attr_accessor :seo_support
13
+ attr_accessor :cache_support
14
+
15
+ attr_accessor :captcha_error_message
16
+ attr_accessor :no_contact_info_error_message
17
+
18
+ attr_accessor :recreate_contact_message_action
19
+
20
+ attr_accessor :breadcrumbs_on_rails_support
21
+
22
+ attr_accessor :recaptcha_support
23
+ attr_accessor :simple_captcha_support
24
+
25
+ attr_accessor :model_settings_support
26
+ attr_accessor :user_abilities_support
27
+ attr_accessor :ra_comments_support
28
+
29
+ def initialize
30
+ @recaptcha_support = !!defined?(Recaptcha)
31
+ @simple_captcha_support = !!defined?(SimpleCaptcha)
32
+
33
+ @captcha = @recaptcha_support || @simple_captcha_support
34
+ @message_required = true
35
+
36
+ @seo_support = !!defined? Hancock::Seo
37
+ @cache_support = !!defined?(Hancock::Cache)
38
+
39
+ @captcha_error_message = "Код проверки введен неверно"
40
+ @no_contact_info_error_message = "Пожалуйста введите Ваш e-mail или телефон, чтобы мы могли связаться с вами."
41
+
42
+ @recreate_contact_message_action = "new"
43
+
44
+ @breadcrumbs_on_rails_support = !!defined?(BreadcrumbsOnRails)
45
+
46
+ @model_settings_support = !!defined?(RailsAdminModelSettings)
47
+ @user_abilities_support = !!defined?(RailsAdminUserAbilities)
48
+ @ra_comments_support = !!defined?(RailsAdminComments)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,154 @@
1
+ module Hancock::Feedback
2
+ module Controllers
3
+ module Contacts
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ if Hancock::Feedback.config.breadcrumbs_on_rails_support
8
+ add_breadcrumb I18n.t('hancock.breadcrumbs.contacts'), :hancock_feedback_contacts_path
9
+ end
10
+
11
+ helper_method :hancock_feedback_update_captcha_path
12
+ end
13
+
14
+ def index
15
+ @contact_message = model.new
16
+ after_initialize
17
+ render locals: locals unless xhr_checker
18
+ end
19
+
20
+ def new
21
+ @contact_message = model.new
22
+ after_initialize
23
+ render locals: locals unless xhr_checker
24
+ end
25
+
26
+ def create
27
+ @contact_message = model.new(message_params)
28
+ after_initialize
29
+
30
+ if Hancock::Feedback.config.captcha
31
+ if Hancock::Feedback.config.recaptcha_support
32
+ if verify_recaptcha
33
+ meth = :save
34
+ else
35
+ meth = :valid?
36
+ @recaptcha_error = I18n.t('hancock.errors.feedback.recaptcha')
37
+ end
38
+
39
+ elsif Hancock::Feedback.config.simple_captcha_support
40
+ meth = :save_with_captcha
41
+
42
+ else
43
+ meth = :save
44
+ end
45
+ else
46
+ meth = :save
47
+ end
48
+
49
+ if @contact_message.send(meth) and @recaptcha_error.blank?
50
+ after_create
51
+ if request.xhr? && process_ajax
52
+ ajax_success
53
+ else
54
+ redirect_after_done
55
+ end
56
+ else
57
+ render_contacts_error
58
+ end
59
+ end
60
+
61
+ def sent
62
+ end
63
+
64
+ def update_captcha
65
+ render layout: false
66
+ end
67
+
68
+
69
+ def hancock_feedback_update_captcha_path
70
+ url_for(action: :update_captcha, time: Time.new.to_i, only_path: true)
71
+ end
72
+ def is_cache_fields
73
+ cache_fields?
74
+ end
75
+ def cache_fields?
76
+ ['new', 'index'].include? action_name
77
+ end
78
+ def cache_key
79
+ 'hancock_feedback_contacts_fields'.freeze
80
+ end
81
+ def fields_partial
82
+ "hancock/feedback/contacts/#{(Hancock::Feedback.config.model_settings_support ? 'fields_with_settings' : 'fields')}".freeze
83
+ end
84
+ def settings_scope
85
+ if Hancock::Feedback.config.model_settings_support
86
+ model.settings
87
+ elsif defined?(Settings)
88
+ Settings.ns('feedback')
89
+ else
90
+ nil
91
+ end
92
+ end
93
+ def recaptcha_options
94
+ {}
95
+ end
96
+ def locals
97
+ {
98
+ is_cache_fields: is_cache_fields,
99
+ cache_key: cache_key,
100
+ fields_partial: fields_partial,
101
+ settings_scope: settings_scope,
102
+ recaptcha_options: recaptcha_options
103
+ }
104
+ end
105
+
106
+ private
107
+ def render_contacts_error
108
+ if request.xhr? && process_ajax
109
+ render partial: form_partial, status: 422, locals: locals
110
+ # render json: {errors: @contact_message.errors}, status: 422
111
+ else
112
+ flash.now[:alert] = @contact_message.errors.full_messages.join("\n")
113
+ render action: Hancock::Feedback.configuration.recreate_contact_message_action, status: 422, locals: locals
114
+ end
115
+ end
116
+ def process_ajax
117
+ true
118
+ end
119
+ def ajax_success
120
+ render partial: success_partial, locals: locals
121
+ # render json: {ok: true}
122
+ end
123
+ def redirect_after_done
124
+ redirect_to action: :sent
125
+ end
126
+ def xhr_checker
127
+ if request.xhr?
128
+ render layout: false, locals: locals
129
+ return true
130
+ end
131
+ end
132
+ def after_initialize
133
+ end
134
+ def after_create
135
+ # overrideable hook for updating message
136
+ end
137
+ def form_partial
138
+ "hancock/feedback/contacts/form"
139
+ end
140
+ def success_partial
141
+ "hancock/feedback/contacts/success"
142
+ end
143
+ def model
144
+ Hancock::Feedback::ContactMessage
145
+ end
146
+ def message_params
147
+ params.require(model.to_param.gsub("::", "").underscore).permit(
148
+ model.permitted_fields + [:name, :email, :phone, :content, :captcha, :captcha_key]
149
+ )
150
+ end
151
+
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,36 @@
1
+ module Hancock::Feedback
2
+ class Engine < ::Rails::Engine
3
+ # isolate_namespace Hancock::Feedback
4
+
5
+ # rake_tasks do
6
+ # require File.expand_path('../tasks', __FILE__)
7
+ # end
8
+
9
+ # initializer "hancock_cms_feedback.email_defaults" do
10
+ # # Write default email settings to DB so they can be changed.
11
+ #
12
+ # #temp
13
+ # begin
14
+ # if Settings and Settings.table_exists?
15
+ # Settings.default_email_from(default: 'noreply@redrocks.pro')
16
+ # Settings.form_email(default: 'admin@redrocks.pro')
17
+ # Settings.email_topic(default: 'с сайта')
18
+ # end
19
+ # rescue
20
+ # end
21
+ # end
22
+
23
+ config.after_initialize do
24
+ # Write default email settings to DB so they can be changed.
25
+ begin
26
+ if Settings and Settings.table_exists?
27
+ Settings.default_email_from(default: 'noreply@site.domain') unless RailsAdminSettings::Setting.ns("main").where(key: "default_email_from").exists?
28
+ Settings.form_email(default: 'admin@site.domain') unless RailsAdminSettings::Setting.ns("main").where(key: "form_email").exists?
29
+ Settings.email_topic(default: 'с сайта') unless RailsAdminSettings::Setting.ns("main").where(key: "email_topic").exists?
30
+ end
31
+ rescue
32
+ end
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ module Hancock::Feedback
2
+ module Models
3
+ module ActiveRecord
4
+ module ContactMessage
5
+ extend ActiveSupport::Concern
6
+ included do
7
+ has_paper_trail
8
+ validates_lengths_from_database
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,92 @@
1
+ module Hancock::Feedback
2
+ module Models
3
+ module ContactMessage
4
+ extend ActiveSupport::Concern
5
+ include Hancock::Model
6
+
7
+ include Hancock::Feedback.orm_specific('ContactMessage')
8
+
9
+ included do
10
+ if Hancock::Feedback.config.model_settings_support
11
+ include RailsAdminModelSettings::ModelSettingable
12
+ end
13
+
14
+ if Hancock::Feedback.config.simple_captcha_support
15
+ include SimpleCaptcha::ModelHelpers
16
+ apply_simple_captcha message: Hancock::Feedback.configuration.captcha_error_message
17
+ end
18
+
19
+ validates_email_format_of :email, unless: 'email.blank?'
20
+ if Hancock::Feedback.config.message_required
21
+ validates_presence_of :content
22
+ end
23
+ validate do
24
+ if email.blank? && phone.blank?
25
+ errors.add(:email, Hancock::Feedback.configuration.no_contact_info_error_message)
26
+ end
27
+ end
28
+
29
+ after_create do
30
+ mailer_class.send(mailer_method, self).deliver_now if send_emails?
31
+ end
32
+
33
+ def self.rails_admin_name_synonyms
34
+ "Фидбек Фидбэк Обратная связь Контакты Сообщение Сообщения".freeze
35
+ end
36
+ def self.rails_admin_navigation_icon
37
+ 'fa fa-envelope-open-o'.freeze
38
+ end
39
+
40
+ # def self.admin_can_default_actions
41
+ # [:show, :read, :edit, :update].freeze
42
+ # end
43
+ # def self.manager_can_default_actions
44
+ # [:show, :read].freeze
45
+ # end
46
+ def self.admin_cannot_actions
47
+ [:new, :create].freeze
48
+ end
49
+ def self.manager_cannot_actions
50
+ [:new, :create, :edit, :update].freeze
51
+ end
52
+
53
+
54
+ def self.manager_can_add_actions
55
+ ret = []
56
+ ret << :model_settings if Hancock::Feedback.config.model_settings_support
57
+ # ret << :model_accesses if Hancock::Feedback.config.user_abilities_support
58
+ ret += [:comments, :model_comments] if Hancock::Feedback.config.ra_comments_support
59
+ ret.freeze
60
+ end
61
+ def self.rails_admin_add_visible_actions
62
+ ret = []
63
+ ret << :model_settings if Hancock::Feedback.config.model_settings_support
64
+ ret << :model_accesses if Hancock::Feedback.config.user_abilities_support
65
+ ret += [:comments, :model_comments] if Hancock::Feedback.config.ra_comments_support
66
+ ret.freeze
67
+ end
68
+
69
+ def self.permitted_fields
70
+ []
71
+ end
72
+ end
73
+
74
+ def send_emails?
75
+ true
76
+ end
77
+
78
+ def mailer_class
79
+ Hancock::Feedback::ContactMailer
80
+ end
81
+
82
+ def mailer_method
83
+ :new_message_email
84
+ end
85
+
86
+ def permitted_fields
87
+ self.class.permitted_fields
88
+ end
89
+
90
+ end
91
+ end
92
+ end