thecore 1.3.20
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +37 -0
- data/app/controllers/application_controller.rb +2 -0
- data/app/controllers/pages_controller.rb +37 -0
- data/app/jobs/application_job.rb +2 -0
- data/app/models/ability.rb +48 -0
- data/app/models/user.rb +158 -0
- data/app/uploaders/attachment_uploader.rb +27 -0
- data/app/uploaders/image_uploader.rb +27 -0
- data/config/initializers/rails_admin.rb +42 -0
- data/config/initializers/rails_application_config.rb +28 -0
- data/config/initializers/thecore_concern.rb +113 -0
- data/config/locales/en.activerecord.yml +6 -0
- data/config/locales/en.contact_mailer.custom.yml +5 -0
- data/config/locales/en.devise.custom.yml +15 -0
- data/config/locales/en.rails_admin.yml +34 -0
- data/config/locales/en.rollincode.yml +5 -0
- data/config/locales/en.simple_form.yml +31 -0
- data/config/locales/en.yml +31 -0
- data/config/locales/it.activerecord.yml +20 -0
- data/config/locales/it.contact_mailer.custom.yml +5 -0
- data/config/locales/it.devise.custom.yml +15 -0
- data/config/locales/it.rails_admin.yml +34 -0
- data/config/locales/it.rollincode.yml +5 -0
- data/config/locales/it.simple_form.yml +31 -0
- data/config/locales/it.yml +38 -0
- data/config/routes.rb +14 -0
- data/db/migrate/20131103142222_create_friendly_id_slugs.rb +15 -0
- data/db/migrate/20141010133701_devise_create_users.rb +54 -0
- data/db/migrate/20150806091039_add_authentication_token_to_users.rb +6 -0
- data/db/migrate/20150825101810_add_roles_mask_to_users.rb +5 -0
- data/db/migrate/20151201145832_add_external_to_user.rb +5 -0
- data/db/migrate/20160331101901_add_code_to_user.rb +6 -0
- data/db/migrate/20160331101902_add_admin_user.rb +162 -0
- data/db/migrate/20161029154134_remove_friendly_id_slugs.rb +5 -0
- data/db/seeds.rb +1 -0
- data/lib/abilities.rb +20 -0
- data/lib/active_record_extension.rb +145 -0
- data/lib/activerecord_paperclip_extension.rb +21 -0
- data/lib/application_configs.rb +26 -0
- data/lib/application_record_loader.rb +3 -0
- data/lib/backtrace_silencers.rb +7 -0
- data/lib/constants.rb +0 -0
- data/lib/date_format.rb +27 -0
- data/lib/devise_initializer.rb +275 -0
- data/lib/generators/thecore/add_git/USAGE +8 -0
- data/lib/generators/thecore/add_git/add_git_generator.rb +269 -0
- data/lib/generators/thecore/thecorize_app/USAGE +8 -0
- data/lib/generators/thecore/thecorize_app/thecorize_app_generator.rb +37 -0
- data/lib/generators/thecore/thecorize_models/USAGE +9 -0
- data/lib/generators/thecore/thecorize_models/thecorize_models_generator.rb +255 -0
- data/lib/generators/thecore/thecorize_plugin/USAGE +9 -0
- data/lib/generators/thecore/thecorize_plugin/thecorize_plugin_generator.rb +166 -0
- data/lib/inflections.rb +16 -0
- data/lib/integer_extensions.rb +26 -0
- data/lib/kaminari_config.rb +12 -0
- data/lib/mime_types.rb +5 -0
- data/lib/postgresql_drop_replacement.rb +11 -0
- data/lib/string_extensions.rb +14 -0
- data/lib/tasks/thecore_tasks.rake +4 -0
- data/lib/thecore/engine.rb +35 -0
- data/lib/thecore/version.rb +3 -0
- data/lib/thecore.rb +63 -0
- data/lib/thecore_actions.rb +2 -0
- data/lib/thecore_base_roles.rb +2 -0
- data/lib/thecore_rails_admin_bulk_delete_concern.rb +19 -0
- data/lib/thecore_rails_admin_export_concern.rb +19 -0
- data/lib/thecore_rails_admin_main_controller_concern.rb +11 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/bin/setup +29 -0
- data/test/dummy/config/application.rb +25 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +41 -0
- data/test/dummy/config/environments/production.rb +79 -0
- data/test/dummy/config/environments/test.rb +42 -0
- data/test/dummy/config/initializers/assets.rb +11 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/log/development.log +6 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/fixtures/users.yml +11 -0
- data/test/integration/navigation_test.rb +8 -0
- data/test/lib/generators/make_associations_complete_generator_test.rb +14 -0
- data/test/lib/generators/thecoreize_rails_plugin_generator_test.rb +14 -0
- data/test/lib/generators/thecorize_app_generator_test.rb +14 -0
- data/test/models/user_test.rb +7 -0
- data/test/test_helper.rb +20 -0
- data/test/thecore_test.rb +7 -0
- metadata +494 -0
@@ -0,0 +1,255 @@
|
|
1
|
+
require 'rails/generators/named_base'
|
2
|
+
module Thecore
|
3
|
+
class ThecorizeModelsGenerator < Rails::Generators::NamedBase
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
|
6
|
+
def init_constants
|
7
|
+
say "Setting the variables", :green
|
8
|
+
@plugin_path = @destination_stack.first.match(Regexp.new("^.*#{@name}"))[0]
|
9
|
+
@parent_path = File.expand_path("..", @plugin_path)
|
10
|
+
@plugin_parent_name = @parent_path.split(File::SEPARATOR).last
|
11
|
+
@plugin_initializers_dir = File.join(@plugin_path, "config", "initializers")
|
12
|
+
@plugin_models_dir = File.join(@plugin_path, "app", "models")
|
13
|
+
@plugin_lib_file = File.join(@plugin_path, "lib", @name, "engine.rb")
|
14
|
+
Dir.chdir @plugin_models_dir do
|
15
|
+
# Getting all the models that are activerecords:
|
16
|
+
@model_files = Dir.glob("*.rb").map do |model|
|
17
|
+
file = File.join(@plugin_models_dir,model)
|
18
|
+
model if is_applicationrecord?(file)
|
19
|
+
end.compact
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "Replace ActiveRecord::Base with ApplicationRecord"
|
24
|
+
def replace_active_record
|
25
|
+
say "Replace ActiveRecord::Base with ApplicationRecord", :green
|
26
|
+
# For each model in this gem
|
27
|
+
@model_files.each do |entry|
|
28
|
+
# It must be a class and don't have rails_admin declaration
|
29
|
+
file = File.join(@plugin_models_dir, entry)
|
30
|
+
# say "Checking file #{file}", :red
|
31
|
+
if is_activerecord?(file) && !has_rails_admin_declaration?(file)
|
32
|
+
# say "Replacing ActiveRecord::Base into #{entry}", :green
|
33
|
+
# Add rails admin declaration
|
34
|
+
gsub_file file, "ActiveRecord::Base", "ApplicationRecord"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
desc "Add rails_admin declaration only in files which are ActiveRecords and don't already have that declaration"
|
40
|
+
def add_rails_admin_reference
|
41
|
+
say "Add rails_admin declaration only in files which are ActiveRecords and don't already have that declaration", :green
|
42
|
+
# For each model in this gem
|
43
|
+
@model_files.each do |entry|
|
44
|
+
# It must be a class and don't have rails_admin declaration
|
45
|
+
file = File.join(@plugin_models_dir,entry)
|
46
|
+
# say "Checking file #{file}", :red
|
47
|
+
if is_applicationrecord?(file) && !has_rails_admin_declaration?(file)
|
48
|
+
# say "Adding rails_admin to #{entry}", :green
|
49
|
+
# Add rails admin declaration
|
50
|
+
inject_into_file file, before: /^end/ do
|
51
|
+
"
|
52
|
+
RailsAdmin.config do |config|
|
53
|
+
config.model self.name.underscore.capitalize.classify do
|
54
|
+
navigation_label I18n.t('admin.settings.label')
|
55
|
+
navigation_icon 'fa fa-file'
|
56
|
+
end
|
57
|
+
end
|
58
|
+
"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
desc "Completes Belongs To Associations"
|
65
|
+
def complete_belongs_to
|
66
|
+
say "Completes Belongs To Associations", :green
|
67
|
+
# For each model in this gem
|
68
|
+
@model_files.each do |entry|
|
69
|
+
# It must be a class and don't have rails_admin declaration
|
70
|
+
file = File.join(@plugin_models_dir,entry)
|
71
|
+
# say "Checking file #{file}", :red
|
72
|
+
if is_applicationrecord?(file)
|
73
|
+
# say "Adding inverse_of to all belongs_to in #{entry}", :green
|
74
|
+
# belongs_to that don't have inverse_of
|
75
|
+
gsub_file file, /^(?!.*inverse_of.*)^[ \t]*belongs_to.*$/ do |match|
|
76
|
+
match << ", inverse_of: :#{entry.split(".").first.pluralize}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
desc "Add Has Many Associations"
|
83
|
+
def add_has_many
|
84
|
+
say "Add Has Many Associations", :green
|
85
|
+
# For each model in this gem
|
86
|
+
@model_files.each do |entry|
|
87
|
+
file = File.join(@plugin_models_dir,entry)
|
88
|
+
# It must be an activerecord model class
|
89
|
+
if is_applicationrecord?(file)
|
90
|
+
# say "Looking for belongs_to in #{entry} and adding the relevant has_manies", :green
|
91
|
+
|
92
|
+
# Polymorphic must be managed manually
|
93
|
+
File.readlines(file).grep(/^(?!.*polymorphic.*)^[ \t]*belongs_to :(.*),.+/).each do |a|
|
94
|
+
target_association = a[/:(.*?),/,1]
|
95
|
+
# look if the file identified by association .rb exists
|
96
|
+
associated_file = File.join(@plugin_models_dir,"#{target_association}.rb")
|
97
|
+
starting_model = entry.split(".").first.pluralize
|
98
|
+
# say "Found belongs_to association: #{target_association} for the model: #{starting_model}", :green
|
99
|
+
# say "- Looking for model file: #{associated_file}", :green
|
100
|
+
if File.exists?(associated_file)
|
101
|
+
# say "The file in which to add has_many, exists and the has_many does not! #{associated_file}", :green
|
102
|
+
# if true, check that the association is non existent and add the association to that file
|
103
|
+
inject_into_file associated_file, after: " < ApplicationRecord\n" do
|
104
|
+
"\n\t\thas_many :#{starting_model}, inverse_of: :#{target_association}, dependent: :destroy\n"
|
105
|
+
end unless has_has_many_association?(associated_file, starting_model)
|
106
|
+
else
|
107
|
+
# otherwise (the file does not exist) check if the initializer for concerns exists,
|
108
|
+
# For each model in this gem
|
109
|
+
initializer_name = "associations_#{target_association}_concern.rb"
|
110
|
+
initializer initializer_name do
|
111
|
+
"require 'active_support/concern'
|
112
|
+
|
113
|
+
module #{target_association.classify}AssociationsConcern
|
114
|
+
extend ActiveSupport::Concern
|
115
|
+
included do
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# include the extension
|
120
|
+
# #{target_association.classify}.send(:include, #{target_association.classify}AssociationsConcern)
|
121
|
+
"
|
122
|
+
end unless File.exists?(File.join(@plugin_initializers_dir, initializer_name))
|
123
|
+
|
124
|
+
# AGGIUNGO L'INCLUDE
|
125
|
+
say "Adding after_initialize file", :green
|
126
|
+
after_initialize_file_name = "#{@name}_after_initialize.rb"
|
127
|
+
after_initialize_file_fullpath = File.join(@plugin_initializers_dir, after_initialize_file_name)
|
128
|
+
initializer after_initialize_file_name do
|
129
|
+
"Rails.application.configure do\n\tconfig.after_initialize do\n\tend\nend"
|
130
|
+
end unless File.exists?(after_initialize_file_fullpath)
|
131
|
+
inject_into_file after_initialize_file_fullpath, after: "config.after_initialize do\n" do
|
132
|
+
"\n\t\t#{target_association.classify}.send(:include, #{target_association.classify}AssociationsConcern)\n"
|
133
|
+
end
|
134
|
+
|
135
|
+
# then add to it the has_many declaration
|
136
|
+
# TODO: only if it doesn't already exists
|
137
|
+
inject_into_file File.join(@plugin_initializers_dir, initializer_name), after: "included do\n" do
|
138
|
+
"\n\t\thas_many :#{starting_model}, inverse_of: :#{target_association}, dependent: :destroy\n"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
desc "Add Has Many Through Associations"
|
147
|
+
def add_has_many_through
|
148
|
+
say "Add Has Many Through Associations", :green
|
149
|
+
# I'ts just an approximation, but for now it could work
|
150
|
+
@model_files.each do |model|
|
151
|
+
association_model = model.split(".").first
|
152
|
+
file = File.join(@plugin_models_dir,model)
|
153
|
+
# It must be an activerecord model class
|
154
|
+
model_with_belongs_to = File.readlines(file).grep(/^[ \t]*belongs_to :.*$/)
|
155
|
+
if model_with_belongs_to.size == 2
|
156
|
+
if yes?("Is #{model} an association model for a has_many through relation?", :red)
|
157
|
+
# getting both the belongs_to models, find their model files, and add the through to each other
|
158
|
+
left_side = model_with_belongs_to.first[/:(.*?),/,1]
|
159
|
+
right_side = model_with_belongs_to.last[/:(.*?),/,1]
|
160
|
+
# This side of the through
|
161
|
+
inject_into_file File.join(@plugin_models_dir, "#{left_side}.rb"), after: " < ApplicationRecord\n" do
|
162
|
+
#has_many :rooms, through: :chosen_rooms, inverse_of: :chosen_decks
|
163
|
+
"
|
164
|
+
has_many :#{right_side.pluralize}, through: :#{association_model.pluralize}, inverse_of: :#{left_side.pluralize}
|
165
|
+
"
|
166
|
+
end unless is_has_many_through? file, right_side.pluralize, association_model.pluralize
|
167
|
+
# Other side of the through
|
168
|
+
inject_into_file File.join(@plugin_models_dir, "#{right_side}.rb"), after: " < ApplicationRecord\n" do
|
169
|
+
#has_many :rooms, through: :chosen_rooms, inverse_of: :chosen_decks
|
170
|
+
"
|
171
|
+
has_many :#{left_side.pluralize}, through: :#{association_model.pluralize}, inverse_of: :#{right_side.pluralize}
|
172
|
+
"
|
173
|
+
end unless is_has_many_through? file, left_side.pluralize, association_model.pluralize
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
desc "Detect polymorphic Associations"
|
180
|
+
def detect_polymorphic_associations
|
181
|
+
say "Detect polymorphic Associations", :green
|
182
|
+
# For each model in this gem
|
183
|
+
# say "MODEL FILES: #{@model_files.inspect} "
|
184
|
+
@model_files.each do |model|
|
185
|
+
file = File.join(@plugin_models_dir,model)
|
186
|
+
# It must be an activerecord model class
|
187
|
+
# belongs_to :rowable, polymorphic: true, inverse_of: :rows
|
188
|
+
polymorphics = File.readlines(file).grep(/^[ \t]*belongs_to :.*polymorphic.*/)
|
189
|
+
polymorphics.each do |polymorphic_belongs_to|
|
190
|
+
polymorphic_target_association = polymorphic_belongs_to[/:(.*?),/,1]
|
191
|
+
# Just keeping the models that are not this model, and
|
192
|
+
answers = ask_question_multiple_choice @model_files.reject {|m| m == model || has_polymorphic_has_many?(File.join(@plugin_models_dir,m), polymorphic_target_association)}, "Where do you want to add the polymorphic has_many called #{polymorphic_target_association} found in #{model}?"
|
193
|
+
answers.each do |answer|
|
194
|
+
# Add the polymorphic has_name declaration
|
195
|
+
inject_into_file File.join(@plugin_models_dir, answer), after: " < ApplicationRecord\n" do
|
196
|
+
"
|
197
|
+
has_many :#{model.split(".").first.pluralize}, as: :#{polymorphic_target_association}, inverse_of: :#{answer.split(".").first.singularize}, dependent: :destroy
|
198
|
+
"
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
private
|
206
|
+
|
207
|
+
def ask_question_multiple_choice models, question = "Choose among one of these, please."
|
208
|
+
return [] if models.empty?
|
209
|
+
# raccolgo tutte le risposte che non siano cancel
|
210
|
+
# e ritorno l'array
|
211
|
+
return_array = []
|
212
|
+
while (answer ||= "") != "cancel"
|
213
|
+
remaining_models = (models-return_array)
|
214
|
+
break if remaining_models.empty?
|
215
|
+
answer = ask question, :red, limited_to: remaining_models.push("cancel").uniq
|
216
|
+
break if answer == "cancel"
|
217
|
+
return_array.push answer
|
218
|
+
end
|
219
|
+
return return_array
|
220
|
+
end
|
221
|
+
|
222
|
+
def is_has_many_through? file, assoc, through
|
223
|
+
(File.readlines(file).grep(/^[ \t]*has_many[ \t]+:#{assoc},[ \t]+through:[ \t]+:#{through}.*/).size > 0) rescue false
|
224
|
+
end
|
225
|
+
|
226
|
+
def has_polymorphic_has_many? file, polymorphic_name
|
227
|
+
(File.readlines(file).grep(/^[ \t]*has_many.+as: :#{polymorphic_name}.*/).size > 0) rescue false
|
228
|
+
end
|
229
|
+
|
230
|
+
def is_activerecord? file
|
231
|
+
(File.readlines(file).grep(/^class [A-Za-z0-9]+ < ActiveRecord::Base/).size > 0) rescue false
|
232
|
+
end
|
233
|
+
|
234
|
+
def is_applicationrecord? file
|
235
|
+
(File.readlines(file).grep(/^class [A-Za-z0-9]+ < ApplicationRecord/).size > 0) rescue false
|
236
|
+
end
|
237
|
+
|
238
|
+
def has_rails_admin_declaration? file
|
239
|
+
(File.readlines(file).grep(/^[ \t]*RailsAdmin.config do/).size > 0) rescue false
|
240
|
+
end
|
241
|
+
|
242
|
+
def is_engine? file
|
243
|
+
(File.readlines(file).grep(/^[ \t]*class Engine < ::Rails::Engine/).size > 0) rescue false
|
244
|
+
end
|
245
|
+
|
246
|
+
def has_add_to_migrations_declaration? file
|
247
|
+
(File.readlines(file).grep(/^[ \t]*initializer '[a-zA-Z0-9]+\.add_to_migrations' do \|app\|/).size > 0) rescue false
|
248
|
+
end
|
249
|
+
|
250
|
+
def has_has_many_association? file, assoc
|
251
|
+
reg_def = "^[ \\t]+has_many[ \\t]+:#{assoc}"
|
252
|
+
(File.readlines(file).grep(Regexp.new(reg_def)).size > 0) rescue false
|
253
|
+
end
|
254
|
+
end
|
255
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
Description:
|
2
|
+
Generator used to change all the models and make them thecore compatible
|
3
|
+
|
4
|
+
Example:
|
5
|
+
rails generate thecorize_plugin pluginname
|
6
|
+
|
7
|
+
This will create:
|
8
|
+
It will change models and adds rails_admin declaration (a general one)
|
9
|
+
It will add all the necessary tidbits to associations (adds to belongs_to, tries to infer has_many's)
|
@@ -0,0 +1,166 @@
|
|
1
|
+
require 'rails/generators/named_base'
|
2
|
+
module Thecore
|
3
|
+
class ThecorizePluginGenerator < Rails::Generators::NamedBase
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
|
6
|
+
# Something like:
|
7
|
+
#
|
8
|
+
class_option :git_server, aliases: "-g"
|
9
|
+
|
10
|
+
def init_constants
|
11
|
+
say "Setting the variables", :green
|
12
|
+
@plugin_path = @destination_stack.first.match(Regexp.new("^.*#{@name}"))[0]
|
13
|
+
@parent_path = File.expand_path("..", @plugin_path)
|
14
|
+
@plugin_parent_name = @parent_path.split(File::SEPARATOR).last
|
15
|
+
@plugin_initializers_dir = File.join(@plugin_path, "config", "initializers")
|
16
|
+
@plugin_models_dir = File.join(@plugin_path, "app", "models")
|
17
|
+
@plugin_lib_file = File.join(@plugin_path, "lib", @name, "engine.rb")
|
18
|
+
Dir.chdir @plugin_models_dir do
|
19
|
+
# Getting all the models that are activerecords:
|
20
|
+
@model_files = Dir.glob("*.rb").map do |model|
|
21
|
+
file = File.join(@plugin_models_dir,model)
|
22
|
+
model if is_applicationrecord?(file)
|
23
|
+
end.compact
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "Make migrations usable into main app"
|
28
|
+
def migrations_to_main_app
|
29
|
+
say "Checking if it's an engine"
|
30
|
+
if is_engine?(@plugin_lib_file) && !has_add_to_migrations_declaration?(@plugin_lib_file)
|
31
|
+
say "Adding migration reflection into engine.rb of", :green
|
32
|
+
inject_into_file @plugin_lib_file, after: "class Engine < ::Rails::Engine\n" do
|
33
|
+
"
|
34
|
+
initializer '#{@name}.add_to_migrations' do |app|
|
35
|
+
unless app.root.to_s == root.to_s
|
36
|
+
# APPEND TO MAIN APP MIGRATIONS FROM THIS GEM
|
37
|
+
config.paths['db/migrate'].expanded.each do |expanded_path|
|
38
|
+
app.config.paths['db/migrate'] << expanded_path
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
desc "require thecore"
|
49
|
+
def add_require_thecore
|
50
|
+
inject_into_file "lib/#{@name}.rb", before: "require \"#{@name}/engine\"" do
|
51
|
+
"
|
52
|
+
require 'thecore'
|
53
|
+
"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
desc "Add Ability File"
|
58
|
+
def add_ability_file
|
59
|
+
# do this just the first time
|
60
|
+
say "Adding abilities file", :green
|
61
|
+
abilities_file_name = "abilities_#{@name}_concern.rb"
|
62
|
+
abilities_file_fullpath = File.join(@plugin_initializers_dir, abilities_file_name)
|
63
|
+
initializer abilities_file_name do
|
64
|
+
"
|
65
|
+
require 'active_support/concern'
|
66
|
+
|
67
|
+
module #{@name.classify}AbilitiesConcern
|
68
|
+
extend ActiveSupport::Concern
|
69
|
+
included do
|
70
|
+
def #{@name}_abilities user
|
71
|
+
if user
|
72
|
+
# if the user is logged in, it can do certain tasks regardless his role
|
73
|
+
if user.admin?
|
74
|
+
# if the user is an admin, it can do a lot of things, usually
|
75
|
+
end
|
76
|
+
|
77
|
+
if user.has_role? :role
|
78
|
+
# a specific role, brings specific powers
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# include the extension
|
86
|
+
TheCoreAbilities.send(:include, #{@name.classify}AbilitiesConcern)
|
87
|
+
"
|
88
|
+
end unless File.exists?(abilities_file_fullpath)
|
89
|
+
end
|
90
|
+
|
91
|
+
desc "Add after_initialize File"
|
92
|
+
def add_after_initialize_file
|
93
|
+
# do this just the first time
|
94
|
+
say "Adding after_initialize file", :green
|
95
|
+
after_initialize_file_name = "#{@name}_after_initialize.rb"
|
96
|
+
after_initialize_file_fullpath = File.join(@plugin_initializers_dir, after_initialize_file_name)
|
97
|
+
initializer after_initialize_file_name do
|
98
|
+
"
|
99
|
+
Rails.application.configure do
|
100
|
+
config.after_initialize do
|
101
|
+
end
|
102
|
+
end
|
103
|
+
"
|
104
|
+
end unless File.exists?(after_initialize_file_fullpath)
|
105
|
+
end
|
106
|
+
|
107
|
+
def manage_git
|
108
|
+
say "Manage Git", :green
|
109
|
+
rails_command "g thecore:add_git #{@name}"
|
110
|
+
end
|
111
|
+
|
112
|
+
def thecoreize_the_models
|
113
|
+
rails_command "g thecore:thecorize_models #{@name}"
|
114
|
+
end
|
115
|
+
|
116
|
+
private
|
117
|
+
|
118
|
+
def ask_question_multiple_choice models, question = "Choose among one of these, please."
|
119
|
+
return [] if models.empty?
|
120
|
+
# raccolgo tutte le risposte che non siano cancel
|
121
|
+
# e ritorno l'array
|
122
|
+
return_array = []
|
123
|
+
while (answer ||= "") != "cancel"
|
124
|
+
remaining_models = (models-return_array)
|
125
|
+
break if remaining_models.empty?
|
126
|
+
answer = ask question, :red, limited_to: remaining_models.push("cancel").uniq
|
127
|
+
break if answer == "cancel"
|
128
|
+
return_array.push answer
|
129
|
+
end
|
130
|
+
return return_array
|
131
|
+
end
|
132
|
+
|
133
|
+
def is_has_many_through? file, assoc, through
|
134
|
+
(File.readlines(file).grep(/^[ \t]*has_many[ \t]+:#{assoc},[ \t]+through:[ \t]+:#{through}.*/).size > 0) rescue false
|
135
|
+
end
|
136
|
+
|
137
|
+
def has_polymorphic_has_many? file, polymorphic_name
|
138
|
+
(File.readlines(file).grep(/^[ \t]*has_many.+as: :#{polymorphic_name}.*/).size > 0) rescue false
|
139
|
+
end
|
140
|
+
|
141
|
+
def is_activerecord? file
|
142
|
+
(File.readlines(file).grep(/^class [A-Za-z0-9]+ < ActiveRecord::Base/).size > 0) rescue false
|
143
|
+
end
|
144
|
+
|
145
|
+
def is_applicationrecord? file
|
146
|
+
(File.readlines(file).grep(/^class [A-Za-z0-9]+ < ApplicationRecord/).size > 0) rescue false
|
147
|
+
end
|
148
|
+
|
149
|
+
def has_rails_admin_declaration? file
|
150
|
+
(File.readlines(file).grep(/^[ \t]*rails_admin do/).size > 0) rescue false
|
151
|
+
end
|
152
|
+
|
153
|
+
def is_engine? file
|
154
|
+
(File.readlines(file).grep(/^[ \t]*class Engine < ::Rails::Engine/).size > 0) rescue false
|
155
|
+
end
|
156
|
+
|
157
|
+
def has_add_to_migrations_declaration? file
|
158
|
+
(File.readlines(file).grep(/^[ \t]*initializer '[a-zA-Z0-9]+\.add_to_migrations' do \|app\|/).size > 0) rescue false
|
159
|
+
end
|
160
|
+
|
161
|
+
def has_has_many_association? file, assoc
|
162
|
+
reg_def = "^[ \\t]+has_many[ \\t]+:#{assoc}"
|
163
|
+
(File.readlines(file).grep(Regexp.new(reg_def)).size > 0) rescue false
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
data/lib/inflections.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file.
|
2
|
+
|
3
|
+
# Add new inflection rules using the following format. Inflections
|
4
|
+
# are locale specific, and you may define rules for as many different
|
5
|
+
# locales as you wish. All of these examples are active by default:
|
6
|
+
# ActiveSupport::Inflector.inflections(:en) do |inflect|
|
7
|
+
# inflect.plural /^(ox)$/i, '\1en'
|
8
|
+
# inflect.singular /^(ox)en/i, '\1'
|
9
|
+
# inflect.irregular 'person', 'people'
|
10
|
+
# inflect.uncountable %w( fish sheep )
|
11
|
+
# end
|
12
|
+
|
13
|
+
# These inflection rules are supported but not enabled by default:
|
14
|
+
# ActiveSupport::Inflector.inflections(:en) do |inflect|
|
15
|
+
# inflect.acronym 'RESTful'
|
16
|
+
# end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module FixnumConcern
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
included do
|
6
|
+
def to_tiered_times skip_seconds = false
|
7
|
+
# Actual calculations
|
8
|
+
mm, ss = self.divmod(60)
|
9
|
+
hh, mm = mm.divmod(60)
|
10
|
+
dd, hh = hh.divmod(24)
|
11
|
+
|
12
|
+
# Presentation
|
13
|
+
sentence = []
|
14
|
+
sentence << I18n.t("tiered_times.dd", count: dd) unless dd.zero?
|
15
|
+
sentence << I18n.t("tiered_times.hh", count: hh) unless hh.zero?
|
16
|
+
sentence << I18n.t("tiered_times.mm", count: mm) unless mm.zero?
|
17
|
+
sentence << I18n.t("tiered_times.ss", count: ss) if !ss.zero? && !skip_seconds
|
18
|
+
|
19
|
+
# to_sentence è una estensione rails che traduce nella forma più corretta (decisamente migliore del join(", "))
|
20
|
+
sentence.to_sentence
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# include the extension
|
26
|
+
Integer.send(:include, FixnumConcern)
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'kaminari'
|
2
|
+
|
3
|
+
Kaminari.configure do |config|
|
4
|
+
# config.default_per_page = 25
|
5
|
+
# config.max_per_page = nil
|
6
|
+
# config.window = 4
|
7
|
+
# config.outer_window = 0
|
8
|
+
# config.left = 0
|
9
|
+
# config.right = 0
|
10
|
+
# config.page_method_name = :page
|
11
|
+
# config.param_name = :page
|
12
|
+
end
|
data/lib/mime_types.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Tasks
|
3
|
+
class PostgreSQLDatabaseTasks
|
4
|
+
def drop
|
5
|
+
establish_master_connection
|
6
|
+
connection.select_all "select pg_terminate_backend(pg_stat_activity.pid) from pg_stat_activity where datname='#{configuration['database']}' AND state='idle';"
|
7
|
+
connection.drop_database configuration['database']
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# .gsub(/[^A-Za-z0-9]/, " ").split.join("%")
|
2
|
+
require 'active_support/concern'
|
3
|
+
|
4
|
+
module StringConcern
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def likeize
|
8
|
+
# .select{|w| w.length > 2}
|
9
|
+
strip.gsub(/[^A-Za-z0-9]/, " ").split.join("%")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# include the extension
|
14
|
+
String.send(:include, StringConcern)
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Thecore
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
initializer "thecore.configure_rails_initialization", group: :all do |app|
|
4
|
+
# Engine configures Rails app here
|
5
|
+
app.config.api_only = false
|
6
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
7
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
8
|
+
app.config.time_zone = 'Rome'
|
9
|
+
|
10
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
11
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
12
|
+
app.config.i18n.default_locale = :it
|
13
|
+
|
14
|
+
# Assets
|
15
|
+
# app.config.assets.precompile += %w( thecore/ie.js )
|
16
|
+
# app.config.assets.precompile += %w( thecore/thecore.js )
|
17
|
+
# app.config.assets.precompile += %w( thecore/thecore.css )
|
18
|
+
# app.config.assets.precompile += %w( thecore/app-logo.png )
|
19
|
+
# app.config.assets.precompile += %w( thecore/apple-touch-icon-ipad-76x76.png )
|
20
|
+
# app.config.assets.precompile += %w( thecore/apple-touch-icon-ipad-retina-152x152.png )
|
21
|
+
# app.config.assets.precompile += %w( thecore/apple-touch-icon-iphone-60x60.png )
|
22
|
+
# app.config.assets.precompile += %w( thecore/apple-touch-icon-iphone-retina-120x120.png )
|
23
|
+
end
|
24
|
+
|
25
|
+
# appending migrations to the main app's ones
|
26
|
+
initializer "thecore.add_to_migrations" do |app|
|
27
|
+
unless app.root.to_s == root.to_s
|
28
|
+
# APPEND TO MAIN APP MIGRATIONS FROM THIS GEM
|
29
|
+
config.paths["db/migrate"].expanded.each do |expanded_path|
|
30
|
+
app.config.paths["db/migrate"] << expanded_path
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/thecore.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'rails/application'
|
3
|
+
require 'application_record_loader'
|
4
|
+
require 'thecore_base_roles'
|
5
|
+
require 'active_record_extension'
|
6
|
+
require 'integer_extensions'
|
7
|
+
require 'string_extensions'
|
8
|
+
require 'activerecord_paperclip_extension'
|
9
|
+
require 'abilities'
|
10
|
+
require 'application_configs'
|
11
|
+
require 'backtrace_silencers'
|
12
|
+
require 'constants'
|
13
|
+
require 'date_format'
|
14
|
+
require 'inflections'
|
15
|
+
require 'mime_types'
|
16
|
+
require 'postgresql_drop_replacement'
|
17
|
+
|
18
|
+
# require 'rails_admin_clone'
|
19
|
+
require 'rails_admin_rollincode'
|
20
|
+
require 'rails_admin'
|
21
|
+
require 'rails_admin_toggleable'
|
22
|
+
require 'rails_admin-i18n'
|
23
|
+
# require 'rails_admin_initializer'
|
24
|
+
# require 'rails_admin_history_rollback'
|
25
|
+
require 'serviceworker-rails'
|
26
|
+
|
27
|
+
require 'rails-i18n'
|
28
|
+
require 'kaminari'
|
29
|
+
require 'kaminari_config'
|
30
|
+
require 'devise'
|
31
|
+
require 'devise_initializer'
|
32
|
+
# require 'rails_admin_amoeba_dup'
|
33
|
+
require 'devise-i18n'
|
34
|
+
require 'devise-i18n-views'
|
35
|
+
require 'cancancan'
|
36
|
+
require 'http_accept_language'
|
37
|
+
require 'bootstrap-sass'
|
38
|
+
require 'font-awesome-sass'
|
39
|
+
# require 'friendly_id'
|
40
|
+
require 'paperclip'
|
41
|
+
|
42
|
+
require 'thecore_rails_admin_export_concern'
|
43
|
+
require 'thecore_rails_admin_bulk_delete_concern'
|
44
|
+
|
45
|
+
require 'oj'
|
46
|
+
require 'multi_json'
|
47
|
+
|
48
|
+
require 'thecore/engine'
|
49
|
+
|
50
|
+
# require 'controllers/application_controller'
|
51
|
+
# require 'controllers/pages_controller'
|
52
|
+
#
|
53
|
+
# require 'jobs/application_job'
|
54
|
+
#
|
55
|
+
# require 'models/ability'
|
56
|
+
# require 'models/application_record'
|
57
|
+
# require 'models/user'
|
58
|
+
#
|
59
|
+
# require 'uploaders/attachment_uploader'
|
60
|
+
# require 'uploaders/image_uploader'
|
61
|
+
|
62
|
+
module Thecore
|
63
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module BulkDeleteConcern
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
included do
|
6
|
+
# Should the action be visible
|
7
|
+
# Edit By taris, it shows the button only if there are records displayed
|
8
|
+
register_instance_option :visible? do
|
9
|
+
# If not in index, then return true,
|
10
|
+
# otherwise it wont' be added to the list
|
11
|
+
# of all Actions during rails initialization
|
12
|
+
# In index, instead, I show it only if there are records in the current view
|
13
|
+
bindings[:controller].action_name == "index" ? (authorized? && !bindings[:controller].instance_variable_get("@objects").blank?) : true
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# include the extension
|
19
|
+
RailsAdmin::Config::Actions::BulkDelete.send(:include, BulkDeleteConcern)
|