alchemy-ajax-form 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +310 -0
  4. data/Rakefile +36 -0
  5. data/app/assets/config/alchemy_ajax_form_manifest.js +3 -0
  6. data/app/assets/images/ajax_form.png +0 -0
  7. data/app/assets/images/alchemy/ajax_form.png +0 -0
  8. data/app/assets/javascripts/ajax_forms.js.erb +185 -0
  9. data/app/controllers/alchemy/admin/ajax_forms_controller.rb +53 -0
  10. data/app/controllers/alchemy/ajax_forms_controller.rb +34 -0
  11. data/app/mailers/alchemy/ajax_forms_mailer.rb +42 -0
  12. data/app/models/alchemy/ajax_form.rb +68 -0
  13. data/app/models/alchemy/ajax_form_ability.rb +22 -0
  14. data/app/views/alchemy/admin/ajax_forms/_table.html.erb +22 -0
  15. data/app/views/alchemy/ajax_forms/create.json.jbuilder +6 -0
  16. data/app/views/alchemy/ajax_forms_mailer/mjml_notify_message.mjml.erb +1 -0
  17. data/app/views/alchemy/ajax_forms_mailer/mjml_notify_user_message.mjml.erb +1 -0
  18. data/app/views/alchemy/ajax_forms_mailer/notify_message.html.erb +1 -0
  19. data/app/views/alchemy/ajax_forms_mailer/notify_user_message.html.erb +1 -0
  20. data/app/views/layouts/alchemy/base_mailer.html.erb +323 -0
  21. data/app/views/layouts/alchemy/mjml_base_mailer.mjml +23 -0
  22. data/config/initializer/assets.rb +1 -0
  23. data/config/locales/it.yml +56 -0
  24. data/config/routes.rb +4 -0
  25. data/lib/alchemy/ajax/form.rb +18 -0
  26. data/lib/alchemy/ajax/form/engine.rb +21 -0
  27. data/lib/alchemy/ajax/form/version.rb +7 -0
  28. data/lib/alchemy/ajax_form_resource.rb +45 -0
  29. data/lib/generators/custom_form/USAGE +8 -0
  30. data/lib/generators/custom_form/custom_form_generator.rb +191 -0
  31. data/lib/generators/custom_form/templates/app/controllers/admin/generic_controller.rb.tt +3 -0
  32. data/lib/generators/custom_form/templates/app/controllers/generic_controller.rb.tt +3 -0
  33. data/lib/generators/custom_form/templates/app/lib/generic_resource.rb.tt +4 -0
  34. data/lib/generators/custom_form/templates/app/model/generic_ability.rb.tt +18 -0
  35. data/lib/generators/custom_form/templates/app/model/generic_form.rb.tt +15 -0
  36. data/lib/generators/custom_form/templates/app/views/alchemy/ajax_forms_mailer/_generic_form.html.erb.tt +38 -0
  37. data/lib/generators/custom_form/templates/app/views/alchemy/ajax_forms_mailer/_generic_user_form.html.erb.tt +39 -0
  38. data/lib/generators/custom_form/templates/app/views/alchemy/elements/_generic_ajax_form_view.html.erb.tt +50 -0
  39. data/lib/generators/custom_form/templates/config/initializers/alchemy_ajax_forms.rb.tt +26 -0
  40. data/lib/generators/custom_form/templates/db/migrate/generic_migration.rb.tt +12 -0
  41. data/lib/tasks/alchemy/ajax/form_tasks.rake +4 -0
  42. metadata +182 -0
@@ -0,0 +1,23 @@
1
+ <mjml>
2
+ <mj-head>
3
+ <mj-font name="Open Sans" href="https://fonts.google.com/specimen/Open+Sans?selection.family=Open+Sans"></mj-font>
4
+ </mj-head>
5
+ <mj-body background-color="#F4F4F4">
6
+ <mj-section background-color="#333b51" background-repeat="no-repeat" text-align="center" vertical-align="top">
7
+ <mj-column>
8
+ <mj-text align="left" font-size="13px" line-height="22px" padding-bottom="0px" padding-top="0px" padding="10px 25px"></mj-text>
9
+ </mj-column>
10
+ </mj-section>
11
+ <mj-section background-color="#ffffff" background-repeat="no-repeat" text-align="center" vertical-align="top" padding="0 0 0 0">
12
+ <%= yield %>
13
+ </mj-section>
14
+ <mj-section background-color="#333b51" background-repeat="no-repeat" text-align="center" vertical-align="top" padding-bottom="20px" padding="0 0 0 0">
15
+ <mj-column>
16
+ <mj-text align="left" font-size="13px" line-height="22px" padding-bottom="0px" padding-top="5px" padding="10px 25px">
17
+ <p style="line-height: 16px; text-align: center; margin: 10px 0;font-size:12px;">
18
+ </p>
19
+ </mj-text>
20
+ </mj-column>
21
+ </mj-section>
22
+ </mj-body>
23
+ </mjml>
@@ -0,0 +1 @@
1
+ Rails.application.config.assets.precompile << 'alchemy_ajax_form_manifest.js'
@@ -0,0 +1,56 @@
1
+ # Files in the config/locales directory are used for internationalization
2
+ # and are automatically loaded by Rails. If you want to use locales other
3
+ # than English, add the necessary files in this directory.
4
+ #
5
+ # To use the locales, use `I18n.t`:
6
+ #
7
+ # I18n.t 'hello'
8
+ #
9
+ # In views, this is aliased to just `t`:
10
+ #
11
+ # <%= t('hello') %>
12
+ #
13
+ # To use a different locale, set it with `I18n.locale`:
14
+ #
15
+ # I18n.locale = :es
16
+ #
17
+ # This would use the information in config/locales/es.yml.
18
+ #
19
+ # The following keys must be escaped otherwise they will not be retrieved by
20
+ # the default I18n backend:
21
+ #
22
+ # true, false, on, off, yes, no
23
+ #
24
+ # Instead, surround them with single quotes.
25
+ #
26
+ # en:
27
+ # 'true': 'foo'
28
+ #
29
+ # To learn more, please read the Rails Internationalization guide
30
+ # available at http://guides.rubyonrails.org/i18n.html.
31
+
32
+ it:
33
+
34
+ activerecord:
35
+ models:
36
+ ajax_form:
37
+ one: Form Ajax
38
+ other: Forms Ajax
39
+
40
+ alchemy:
41
+ modules:
42
+ ajax_form: "Form Contatti"
43
+
44
+
45
+ ajax_form_mailer:
46
+ subjects:
47
+ default_notification_object: "Nuova mail dal sito web"
48
+
49
+ privacy_string: "Accosenti al trattamento dei tuoi dati in base al D.Lgs
50
+ 30/06/2003 n.196"
51
+ submit: "Invia"
52
+ form_sended_succesfully: "Inviato con successo"
53
+
54
+
55
+
56
+
@@ -0,0 +1,4 @@
1
+ Rails.application.routes.draw do
2
+
3
+
4
+ end
@@ -0,0 +1,18 @@
1
+
2
+ require "alchemy_cms"
3
+ require "js-routes"
4
+ require "recaptcha/rails"
5
+ require "mjml-rails"
6
+ require "recaptcha"
7
+ require "alchemy/ajax/form/engine"
8
+
9
+ module Alchemy
10
+ module Ajax
11
+ module Form
12
+ # Your code goes here...
13
+ mattr_accessor :enable_mjml
14
+ @@enable_mjml = true
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ module Alchemy
2
+ module Ajax
3
+ module Form
4
+ class Engine < ::Rails::Engine
5
+
6
+ isolate_namespace Alchemy::Ajax::Form
7
+
8
+ config.autoload_paths << config.root.join('lib')
9
+
10
+ initializer "alchemy_ajax_form.assets.precompile" do |app|
11
+ app.config.assets.precompile << 'alchemy_ajax_form_manifest.js'
12
+ end
13
+
14
+
15
+ # initializer "alchemy_ajax_form.assets.precompile" do |app|
16
+ # app.config.assets.precompile << %w(alchemy/ajax_form.png)
17
+ # end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ module Alchemy
2
+ module Ajax
3
+ module Form
4
+ VERSION = '1.0.0'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,45 @@
1
+ module Alchemy
2
+ class AjaxFormResource < Resource
3
+
4
+
5
+ def attributes
6
+ attr = super.reject {|c| [:check_privacy,:language_id].include?(c[:name].to_sym)}
7
+
8
+ attr += [{
9
+ name: :language,
10
+ relation: {
11
+ name: :language,
12
+ model_association: Alchemy::Language,
13
+ attr_method: :name
14
+
15
+ }
16
+
17
+ },
18
+ {
19
+ name: :site,
20
+ relation: {
21
+ name: :site,
22
+ model_association: Alchemy::Site,
23
+ attr_method: :name
24
+
25
+ }
26
+
27
+ }]
28
+ end
29
+
30
+
31
+ def searchable_attribute_names
32
+ if model.column_names.include? "email"
33
+ [:email]
34
+ else
35
+ []
36
+ end
37
+ end
38
+
39
+ def search_field_name
40
+ searchable_attribute_names.join("_or_") + "_cont"
41
+ end
42
+
43
+
44
+ end
45
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate custom_form Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,191 @@
1
+ class CustomFormGenerator < Rails::Generators::NamedBase
2
+ include Rails::Generators::Migration
3
+
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ argument :attributes, type: :array, required: false
7
+
8
+ attr_accessor :model_attributes
9
+
10
+ desc "Create amin controller"
11
+ def add_admin_cotroller
12
+ template "app/controllers/admin/generic_controller.rb.tt", "app/controllers/admin/#{name.underscore.pluralize}_controller.rb"
13
+ end
14
+
15
+ desc "Create controller"
16
+ def add_controller
17
+ template "app/controllers/generic_controller.rb.tt", "app/controllers/#{name.underscore.pluralize}_controller.rb"
18
+ end
19
+
20
+ desc "Create Resource"
21
+ def add_resource
22
+ template "app/lib/generic_resource.rb.tt", "app/lib/#{name.singularize.underscore}_resource.rb"
23
+ end
24
+
25
+ desc "Create model"
26
+ def add_model
27
+
28
+ # inserted_attributes = attributes
29
+ #
30
+ # self.attributes = ["gfgdfgdfg:skdkfjdkf"]
31
+ # parse_attributes!
32
+ #
33
+ # say self.attributes + inserted_attributes
34
+ #
35
+ inserted_attributes = []
36
+
37
+ if attributes.empty?
38
+ ask_attribute
39
+ else
40
+ inserted_attributes = attributes
41
+ self.attributes = []
42
+ ask_attribute "Do you want add attributes?"
43
+ end
44
+ parse_attributes!
45
+ self.attributes = inserted_attributes + self.attributes unless inserted_attributes.empty?
46
+
47
+ migration_template "db/migrate/generic_migration.rb.tt", "db/migrate/create_#{name.singularize.underscore}.rb"
48
+
49
+ template "app/model/generic_form.rb.tt", "app/models/#{name.singularize.underscore}.rb"
50
+ template "app/model/generic_ability.rb.tt", "app/models/#{name.singularize.underscore}_ability.rb"
51
+
52
+ end
53
+
54
+
55
+ desc "Create elements view"
56
+ def add_element_view
57
+
58
+ template "app/views/alchemy/elements/_generic_ajax_form_view.html.erb.tt",
59
+ "app/views/alchemy/elements/_#{name.underscore.singularize}_view.html.erb"
60
+
61
+ end
62
+
63
+ desc "Create initializer"
64
+ def add_initializer
65
+ #
66
+ file ="#{Rails.root}/config/initializers/alchemy_ajax_forms.rb"
67
+ if !File.exist? file
68
+ template "config/initializers/alchemy_ajax_forms.rb.tt","config/initializers/alchemy_ajax_forms.rb"
69
+ else
70
+ insert_into_file file, before: "]" do
71
+ <<-code
72
+ {
73
+ name: 'modules.#{name.underscore.pluralize}',
74
+ controller: '/admin/#{name.underscore.pluralize}',
75
+ action: 'index'
76
+ },
77
+ code
78
+ end
79
+
80
+ append_to_file file do
81
+ "\nAlchemy.register_ability(#{name.singularize.classify}Ability)\n"
82
+ end
83
+
84
+ end
85
+
86
+
87
+
88
+ end
89
+
90
+ desc "Add email template"
91
+ def add_mail_template
92
+ ext = "html.erb"
93
+ ext = "mjml" if Alchemy::Ajax::Form.enable_mjml
94
+ template "app/views/alchemy/ajax_forms_mailer/_generic_form.html.erb.tt",
95
+ "app/views/alchemy/ajax_forms_mailer/_#{name.underscore.singularize}.#{ext}"
96
+ template "app/views/alchemy/ajax_forms_mailer/_generic_user_form.html.erb.tt",
97
+ "app/views/alchemy/ajax_forms_mailer/_#{name.underscore.singularize}_user.#{ext}"
98
+ end
99
+
100
+ desc "Add element configuration"
101
+ def add_element_config
102
+ append_to_file "config/alchemy/elements.yml", <<-element
103
+ - name: #{name.singularize.underscore}
104
+ hint: false
105
+ contents:
106
+ - name: privacy_page
107
+ type: EssenceText
108
+ settings:
109
+ linkable: true
110
+ - name: recipient_notification
111
+ type: EssenceText
112
+ default: "example@example.it"
113
+ - name: send_only
114
+ type: EssenceBoolean
115
+ - name: send_staff
116
+ type: EssenceBoolean
117
+ - name: send_user
118
+ type: EssenceBoolean\n
119
+ element
120
+
121
+ end
122
+
123
+
124
+ desc "Add needed routes"
125
+ def add_routes
126
+
127
+ routes_path = Rails.root + "config/routes.rb"
128
+
129
+ if File.exist?(routes_path) and File.readlines(routes_path).grep(/namespace :admin do/).count > 0
130
+
131
+ insert_into_file routes_path, :after => "namespace :admin do\n" do
132
+ "\nresources :#{name.underscore.pluralize}\n"
133
+
134
+ end
135
+
136
+ route "\nresources :#{name.underscore.pluralize}\n"
137
+
138
+
139
+ else
140
+ route <<-route
141
+ \nnamespace :admin do
142
+ resources :#{name.underscore.pluralize}
143
+
144
+ end
145
+ resources :#{name.underscore.pluralize} , only: [:create]\n
146
+ route
147
+
148
+ end
149
+
150
+ end
151
+
152
+ desc "Require needed assets"
153
+ def add_assets
154
+ inject_into_file "app/assets/javascripts/application.js" , before: '//= require_tree .' do
155
+ "\n//= require jquery3\n//= require ajax_forms\n"
156
+ end
157
+ end
158
+
159
+ desc "Run migration and install mjml and create alchemy elements"
160
+ def run_scripts
161
+ rake("db:migrate")
162
+ #run "npm install mjml"
163
+ generate("alchemy:elements","--skip")
164
+ end
165
+
166
+
167
+ private
168
+
169
+ def ask_attribute mex = nil
170
+ if mex.nil?
171
+ response = ask "Insert an attribute name (q for exit)"
172
+ if response.downcase != "q"
173
+ self.attributes << response unless response.blank?
174
+ ask_attribute
175
+ end
176
+
177
+ else
178
+ response = ask mex, :limited_to => ["y", "n"]
179
+ if response.downcase == "y"
180
+ ask_attribute
181
+ end
182
+ end
183
+ end
184
+
185
+
186
+ def self.next_migration_number(dir)
187
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
188
+ end
189
+
190
+
191
+ end
@@ -0,0 +1,3 @@
1
+ class Admin::<%= name.classify.pluralize %>Controller < Alchemy::Admin::AjaxFormsController
2
+
3
+ end
@@ -0,0 +1,3 @@
1
+ class <%= name.classify.pluralize %>Controller < Alchemy::AjaxFormsController
2
+
3
+ end
@@ -0,0 +1,4 @@
1
+ class <%= name.classify.singularize %>Resource < Alchemy::AjaxFormResource
2
+
3
+
4
+ end
@@ -0,0 +1,18 @@
1
+ class <%= name.singularize.classify%>Ability
2
+ include CanCan::Ability
3
+
4
+ def initialize(user)
5
+
6
+ if user.present? && user.is_admin?
7
+
8
+ can :manage, <%= name.singularize.classify%>
9
+ cannot :create, <%= name.singularize.classify%>
10
+ cannot :update, <%= name.singularize.classify%>
11
+ can :manage, :admin_<%= name.underscore.pluralize %>
12
+ cannot :create, :admin_<%= name.underscore.pluralize %>
13
+ cannot :update, :admin_<%= name.underscore.pluralize %>
14
+
15
+ end
16
+ end
17
+
18
+ end
@@ -0,0 +1,15 @@
1
+ class <%= name.singularize.classify %> < Alchemy::AjaxForm
2
+
3
+ validates :check_privacy, inclusion: [true, '1']
4
+
5
+
6
+ def notify_subject
7
+ I18n.translate(:<%= name.singularize.underscore %>_notify, scope: [:ajax_form_mailer, :subjects])
8
+ end
9
+
10
+ def notify_user_subject
11
+ I18n.translate(:<%= name.singularize.underscore %>_user_notify, scope: [:ajax_form_mailer, :subjects])
12
+ end
13
+
14
+
15
+ end
@@ -0,0 +1,38 @@
1
+ <% if Alchemy::Ajax::Form.enable_mjml %>
2
+ <mj-column>
3
+ <mj-text align="left" color="#55575d" font-family="Arial, sans-serif" font-size="13px" line-height="22px" padding-bottom="0px" padding-top="5px" padding="10px 25px">
4
+ <p style="line-height: 16px; text-align: center; margin: 10px 0;font-size:12px;color:#000;font-family:'Times New Roman',Helvetica,Arial,sans-serif">
5
+ <% attributes.each do |attr| %>
6
+ <span><b><%%= <%= name.singularize.classify %>.human_attribute_name(:<%= attr.name %>) %>:</b></span>
7
+ <span>
8
+ <%%= rec.<%=attr.name %> %>
9
+ </span><br>
10
+ <% end %>
11
+ </p>
12
+ </mj-text>
13
+ </mj-column>
14
+ <% else %>
15
+
16
+ <!--[if mso]><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td style="padding-right: 10px; padding-left: 10px; padding-top: 10px; padding-bottom: 10px; font-family: Arial, sans-serif"><![endif]-->
17
+ <div style="color:#0D0D0D;font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;line-height:1.2;padding-top:10px;padding-right:10px;padding-bottom:10px;padding-left:10px;">
18
+ <div style="font-size: 12px; line-height: 1.2; color: #0D0D0D; font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; mso-line-height-alt: 14px;">
19
+ <!-- put here intro <p style="font-size: 28px; line-height: 1.2; word-break: break-word; text-align: left; mso-line-height-alt: 34px; margin: 0;"><span style="font-size: 28px;"><strong><span style="font-size: 28px;">Hello Username,</span></strong></span><br/><span style="font-size: 28px;">Registration completed</span></p> -->
20
+ </div>
21
+ </div>
22
+ <!--[if mso]></td></tr></table><![endif]-->
23
+ <!--[if mso]><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td style="padding-right: 10px; padding-left: 10px; padding-top: 10px; padding-bottom: 10px; font-family: Arial, sans-serif"><![endif]-->
24
+ <div style="color:#000000;font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;line-height:1.5;padding-top:10px;padding-right:10px;padding-bottom:10px;padding-left:10px;">
25
+ <div style="font-size: 12px; line-height: 1.5; color: #000000; font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; mso-line-height-alt: 18px;">
26
+ <p style="font-size: 14px; line-height: 1.5; word-break: break-word; text-align: left; mso-line-height-alt: 21px; margin: 0;">
27
+
28
+ <% attributes.each do |attr| %>
29
+ <span><b><%%= <%= name.singularize.classify %>.human_attribute_name(:<%= attr.name %>) %>:</b></span>
30
+ <span>
31
+ <%%= rec.<%=attr.name %> %>
32
+ </span><br>
33
+ <% end %>
34
+ </p>
35
+ </div>
36
+ </div>
37
+ <!--[if mso]></td></tr></table><![endif]-->
38
+ <% end %>