sibu 0.8.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +20 -20
  3. data/README.md +38 -38
  4. data/Rakefile +36 -36
  5. data/app/assets/config/sibu_manifest.js +2 -2
  6. data/app/assets/fonts/sibu/Sibu.eot +0 -0
  7. data/app/assets/fonts/sibu/Sibu.svg +13 -13
  8. data/app/assets/fonts/sibu/Sibu.ttf +0 -0
  9. data/app/assets/fonts/sibu/Sibu.woff +0 -0
  10. data/app/assets/javascripts/cropper/cropper.js +3694 -3694
  11. data/app/assets/javascripts/quill/quill.js +11401 -11401
  12. data/app/assets/javascripts/sibu/common.js +6 -6
  13. data/app/assets/javascripts/sibu/sibu.js.erb +155 -155
  14. data/app/assets/javascripts/tabs/van11y-accessible-tab-panel-aria.js +465 -465
  15. data/app/assets/stylesheets/cropper/cropper.css +304 -304
  16. data/app/assets/stylesheets/quill/quill.snow.css +945 -945
  17. data/app/assets/stylesheets/sibu/defaults.scss +336 -336
  18. data/app/assets/stylesheets/sibu/icons.scss +40 -40
  19. data/app/assets/stylesheets/sibu/sibu.css +131 -131
  20. data/app/controllers/sibu/application_controller.rb +14 -14
  21. data/app/controllers/sibu/documents_controller.rb +35 -35
  22. data/app/controllers/sibu/images_controller.rb +78 -78
  23. data/app/controllers/sibu/pages_controller.rb +197 -197
  24. data/app/controllers/sibu/sites_controller.rb +81 -81
  25. data/app/helpers/sibu/application_helper.rb +11 -11
  26. data/app/helpers/sibu/documents_helper.rb +4 -4
  27. data/app/helpers/sibu/images_helper.rb +4 -4
  28. data/app/helpers/sibu/pages_helper.rb +194 -194
  29. data/app/helpers/sibu/sites_helper.rb +23 -23
  30. data/app/jobs/sibu/application_job.rb +4 -4
  31. data/app/mailers/sibu/application_mailer.rb +6 -6
  32. data/app/models/concerns/sibu/document_uploader.rb +8 -8
  33. data/app/models/concerns/sibu/image_uploader.rb +31 -31
  34. data/app/models/concerns/sibu/sections_concern.rb +134 -134
  35. data/app/models/concerns/sibu/style_uploader.rb +1 -1
  36. data/app/models/concerns/sibu/user_concern.rb +8 -8
  37. data/app/models/sibu/application_record.rb +5 -5
  38. data/app/models/sibu/document.rb +16 -16
  39. data/app/models/sibu/dynamic_style.rb +76 -76
  40. data/app/models/sibu/image.rb +10 -19
  41. data/app/models/sibu/page.rb +66 -66
  42. data/app/models/sibu/site.rb +97 -97
  43. data/app/models/sibu/site_template.rb +22 -22
  44. data/app/views/layouts/sibu/application.html.erb +23 -23
  45. data/app/views/layouts/sibu/edit_content.html.erb +262 -262
  46. data/app/views/layouts/sibu/site.html.erb +45 -45
  47. data/app/views/sibu/documents/_form.html.erb +14 -14
  48. data/app/views/sibu/documents/index.html.erb +37 -37
  49. data/app/views/sibu/documents/new.html.erb +12 -12
  50. data/app/views/sibu/images/_edit_form.html.erb +23 -23
  51. data/app/views/sibu/images/_form.html.erb +34 -34
  52. data/app/views/sibu/images/edit.js.erb +2 -2
  53. data/app/views/sibu/images/index.html.erb +31 -13
  54. data/app/views/sibu/images/new.html.erb +12 -12
  55. data/app/views/sibu/images/show.html.erb +2 -2
  56. data/app/views/sibu/pages/_code_edit_panel.html.erb +17 -17
  57. data/app/views/sibu/pages/_element_actions.html.erb +4 -4
  58. data/app/views/sibu/pages/_error_panel.html.erb +4 -4
  59. data/app/views/sibu/pages/_form.html.erb +62 -62
  60. data/app/views/sibu/pages/_group_edit_panel.html.erb +3 -3
  61. data/app/views/sibu/pages/_link_edit_panel.html.erb +50 -50
  62. data/app/views/sibu/pages/_map_edit_panel.html.erb +27 -27
  63. data/app/views/sibu/pages/_media_edit_panel.html.erb +46 -46
  64. data/app/views/sibu/pages/_new_section_panel.html.erb +43 -43
  65. data/app/views/sibu/pages/_paragraph_edit_panel.html.erb +18 -18
  66. data/app/views/sibu/pages/_text_edit_panel.html.erb +19 -19
  67. data/app/views/sibu/pages/child_element.js.erb +6 -6
  68. data/app/views/sibu/pages/clone_element.js.erb +6 -6
  69. data/app/views/sibu/pages/create_section.js.erb +6 -6
  70. data/app/views/sibu/pages/delete_element.js.erb +6 -6
  71. data/app/views/sibu/pages/delete_section.js.erb +6 -6
  72. data/app/views/sibu/pages/destroy.html.erb +2 -2
  73. data/app/views/sibu/pages/edit.html.erb +8 -8
  74. data/app/views/sibu/pages/edit_content.html.erb +3 -3
  75. data/app/views/sibu/pages/edit_element.js.erb +100 -100
  76. data/app/views/sibu/pages/edit_section.js.erb +7 -7
  77. data/app/views/sibu/pages/index.html.erb +40 -40
  78. data/app/views/sibu/pages/new.html.erb +8 -8
  79. data/app/views/sibu/pages/new_section.js.erb +19 -19
  80. data/app/views/sibu/pages/show.html.erb +3 -3
  81. data/app/views/sibu/pages/update.html.erb +2 -2
  82. data/app/views/sibu/pages/update_element.js.erb +5 -5
  83. data/app/views/sibu/pages/update_section.js.erb +5 -5
  84. data/app/views/sibu/sites/_form.html.erb +125 -125
  85. data/app/views/sibu/sites/destroy.html.erb +2 -2
  86. data/app/views/sibu/sites/edit.html.erb +9 -9
  87. data/app/views/sibu/sites/edit_styles.html.erb +8 -8
  88. data/app/views/sibu/sites/index.html.erb +41 -41
  89. data/app/views/sibu/sites/new.html.erb +18 -18
  90. data/app/views/sibu/sites/update.html.erb +2 -2
  91. data/config/initializers/constants.rb +2 -2
  92. data/config/initializers/shrine.rb +18 -18
  93. data/config/routes.rb +30 -30
  94. data/config/tinymce.yml +5 -5
  95. data/db/migrate/20180124095041_create_sibu_sites.rb +12 -12
  96. data/db/migrate/20180124095213_create_sibu_pages.rb +15 -15
  97. data/db/migrate/20180124145030_create_sibu_site_templates.rb +10 -10
  98. data/db/migrate/20180125231638_add_user_id_to_sites.rb +5 -5
  99. data/db/migrate/20180126114522_rename_url_to_path_in_pages.rb +5 -5
  100. data/db/migrate/20180126114628_add_domain_to_sites.rb +5 -5
  101. data/db/migrate/20180127211533_create_sibu_images.rb +11 -11
  102. data/db/migrate/20180208082317_rename_images_user_id_to_site_id.rb +5 -5
  103. data/db/migrate/20180208125024_rename_image_data_column.rb +5 -5
  104. data/db/migrate/20180210181644_add_defaults_to_site_templates.rb +6 -6
  105. data/db/migrate/20180214134653_add_fields_to_sites.rb +5 -5
  106. data/db/migrate/20180227151519_add_default_templates_to_site_templates.rb +5 -5
  107. data/db/migrate/20180301121902_add_style_data_to_sites.rb +5 -5
  108. data/db/migrate/20180301152101_add_default_styles_to_templates.rb +5 -5
  109. data/db/migrate/20180321144021_move_images_to_user_level.rb +7 -7
  110. data/db/migrate/20180321170310_add_version_to_sibu_sites.rb +7 -7
  111. data/db/migrate/20180405095448_create_sibu_documents.rb +10 -10
  112. data/db/migrate/20190110204854_add_custom_data_to_sibu_pages.rb +5 -5
  113. data/lib/sibu.rb +4 -4
  114. data/lib/sibu/engine.rb +19 -19
  115. data/lib/sibu/utils.rb +14 -14
  116. data/lib/sibu/version.rb +3 -3
  117. data/lib/tasks/sibu_tasks.rake +4 -4
  118. metadata +3 -3
@@ -1,4 +1,4 @@
1
- module Sibu
2
- class ApplicationJob < ActiveJob::Base
3
- end
4
- end
1
+ module Sibu
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -1,6 +1,6 @@
1
- module Sibu
2
- class ApplicationMailer < ActionMailer::Base
3
- default from: 'from@example.com'
4
- layout 'mailer'
5
- end
6
- end
1
+ module Sibu
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -1,8 +1,8 @@
1
- class Sibu::DocumentUploader < Shrine
2
- plugin :validation_helpers
3
-
4
- Attacher.validate do
5
- validate_max_size (Rails.application.config.sibu[:max_doc_size] || 10)*1024*1024, message: "est trop volumineux (#{Rails.application.config.sibu[:max_doc_size] || 10}Mo maximum)"
6
- # validate_mime_type_inclusion %w[application/pdf]
7
- end
8
- end
1
+ class Sibu::DocumentUploader < Shrine
2
+ plugin :validation_helpers
3
+
4
+ Attacher.validate do
5
+ validate_max_size (Rails.application.config.sibu[:max_doc_size] || 10)*1024*1024, message: "est trop volumineux (#{Rails.application.config.sibu[:max_doc_size] || 10}Mo maximum)"
6
+ # validate_mime_type_inclusion %w[application/pdf]
7
+ end
8
+ end
@@ -1,32 +1,32 @@
1
- require "image_processing/mini_magick"
2
-
3
- class Sibu::ImageUploader < Shrine
4
- include ImageProcessing::MiniMagick
5
- plugin :processing
6
- plugin :versions
7
- plugin :delete_raw
8
-
9
- process(:store) do |io, context|
10
- original = io.download
11
- images_config = Rails.application.config.sibu[:images]
12
- image_type = io.mime_type
13
-
14
- if image_type == "image/svg+xml" || io.metadata['filename'].end_with?('.svg')
15
- {original: io, large: io.download, medium: io.download, small: io.download}
16
- else
17
- large = resize_to_limit!(original, images_config[:large], images_config[:large]) {|cmd| cmd.auto_orient}
18
- medium = resize_to_limit(large, images_config[:medium], images_config[:medium])
19
- small = resize_to_limit(medium, images_config[:small], images_config[:small])
20
-
21
- {original: io, large: large, medium: medium, small: small}
22
- end
23
- end
24
-
25
- def generate_location(io, context)
26
- user_id = context[:record] ? context[:record].user_id : nil
27
- style = context[:version] != :original ? "resized" : "originals"
28
- name = super
29
-
30
- [user_id, style, name].compact.join("/")
31
- end
1
+ require "image_processing/mini_magick"
2
+
3
+ class Sibu::ImageUploader < Shrine
4
+ include ImageProcessing::MiniMagick
5
+ plugin :processing
6
+ plugin :versions
7
+ plugin :delete_raw
8
+
9
+ process(:store) do |io, context|
10
+ original = io.download
11
+ images_config = Rails.application.config.sibu[:images]
12
+ image_type = io.mime_type
13
+
14
+ if image_type == "image/svg+xml" || io.metadata['filename'].end_with?('.svg')
15
+ {original: io, large: io.download, medium: io.download, small: io.download}
16
+ else
17
+ large = resize_to_limit!(original, images_config[:large], images_config[:large]) {|cmd| cmd.auto_orient}
18
+ medium = resize_to_limit(large, images_config[:medium], images_config[:medium])
19
+ small = resize_to_limit(medium, images_config[:small], images_config[:small])
20
+
21
+ {original: io, large: large, medium: medium, small: small}
22
+ end
23
+ end
24
+
25
+ def generate_location(io, context)
26
+ user_id = context[:record] ? context[:record].user_id : nil
27
+ style = context[:version] != :original ? "resized" : "originals"
28
+ name = super
29
+
30
+ [user_id, style, name].compact.join("/")
31
+ end
32
32
  end
@@ -1,135 +1,135 @@
1
- module Sibu
2
- module SectionsConcern
3
- include ActiveSupport::Concern
4
-
5
- def section(id)
6
- if id
7
- pos = sections.index {|s| s["id"] == id}
8
- pos ? sections[pos] : {"id" => id}
9
- end
10
- end
11
-
12
- def elements(*ids)
13
- unless ids.blank?
14
- s = section(ids.first)
15
- unless s["elements"].blank?
16
- ids[1..-1].each do |elt_id|
17
- pos = s["elements"].index {|e| e["id"] == elt_id.split('|').last}
18
- s = pos ? s["elements"][pos] : {"id" => elt_id, "elements" => []}
19
- end
20
- s["elements"]
21
- end
22
- end
23
- end
24
-
25
- def element(*ids)
26
- unless ids.blank?
27
- if ids.length == 1
28
- section(ids.first)
29
- else
30
- elts = elements(*ids[0..-2]) || []
31
- pos = elts.index {|e| e["id"] == ids.last}
32
- pos ? elts[pos] : {"id" => ids.last}
33
- end
34
- end
35
- end
36
-
37
- def update_element(*ids, value)
38
- unless ids.blank?
39
- parent_section = find_or_init(*ids)["elements"]
40
- if parent_section.any? {|elt| elt["id"] == value["id"]}
41
- parent_section.map! {|elt| elt["id"] == value["id"] ? value : elt}
42
- else
43
- parent_section << value.to_h
44
- end
45
-
46
- value if save
47
- end
48
- end
49
-
50
- def clone_element(*ids, element_id)
51
- src_elt = find_or_init(*ids, element_id)
52
- siblings = find_or_init(*ids)["elements"]
53
- ref_index = siblings.index {|s| s["id"] == element_id}
54
- new_elt = siblings[ref_index].deep_dup
55
- new_elt["id"] = "cl#{Time.current.to_i}"
56
- siblings.insert(ref_index + 1, new_elt)
57
- save ? new_elt : nil
58
- end
59
-
60
- def delete_element(*ids, element_id)
61
- src_elt = find_or_init(*ids, element_id)
62
- siblings = find_or_init(*ids)["elements"]
63
- ref_index = siblings.index {|s| s["id"] == element_id}
64
- siblings.delete_at(ref_index)
65
- save
66
- end
67
-
68
- def child_element(*ids, element_id)
69
- siblings = elements(*ids)
70
- parent_elt = siblings[siblings.index {|s| s["id"] == element_id}]
71
- if parent_elt["elements"].blank?
72
- parent_elt["elements"] = [{"id" => "cl#{Time.current.to_i}"}]
73
- else
74
- parent_elt["elements"] << {"id" => "cl#{Time.current.to_i}"}
75
- end
76
- save
77
- end
78
-
79
- def create_section(*ids, after, new_section)
80
- new_section["id"] = "cs#{Time.current.to_i}"
81
- if ids.length == 1
82
- parent = sections
83
- else
84
- parent = find_or_init(*ids[0..-2])["elements"]
85
- end
86
- if new_section["template"].blank?
87
- nil
88
- else
89
- template_defaults = (site_template.templates && site_template.templates[new_section["template"]]) ? site_template.templates[new_section["template"]] : {}
90
- sec = template_defaults.merge(new_section)
91
- ref_pos = parent.index {|s| s["id"] == ids.last}
92
- parent.insert(after.to_s == 'true' ? ref_pos + 1 : ref_pos, sec)
93
- sec if save
94
- end
95
- end
96
-
97
- def delete_section(*ids)
98
- if ids.length == 1
99
- if sections.length == 1
100
- nil
101
- else
102
- ref_index = sections.index {|s| s["id"] == ids.first}
103
- sections.delete_at(ref_index)
104
- save
105
- end
106
- else
107
- parent = find_or_init(*ids[0..-2])
108
- ref_index = parent["elements"].index {|s| s["id"] == ids.last}
109
- parent["elements"].delete_at(ref_index)
110
- save
111
- end
112
- end
113
-
114
- def elt(siblings, elt_id, default_elt = nil)
115
- pos = siblings.index {|e| e["id"] == elt_id}
116
- pos ? siblings[pos] : default_elt
117
- end
118
-
119
- def find_or_init(*ids)
120
- node = nil
121
- siblings = sections
122
- ids.each do |elt_id|
123
- node = elt(siblings, elt_id)
124
- if node.nil?
125
- node = {"id" => elt_id, "elements" => []}
126
- siblings << node
127
- elsif node["elements"].nil?
128
- node["elements"] = []
129
- end
130
- siblings = node["elements"]
131
- end
132
- node
133
- end
134
- end
1
+ module Sibu
2
+ module SectionsConcern
3
+ include ActiveSupport::Concern
4
+
5
+ def section(id)
6
+ if id
7
+ pos = sections.index {|s| s["id"] == id}
8
+ pos ? sections[pos] : {"id" => id}
9
+ end
10
+ end
11
+
12
+ def elements(*ids)
13
+ unless ids.blank?
14
+ s = section(ids.first)
15
+ unless s["elements"].blank?
16
+ ids[1..-1].each do |elt_id|
17
+ pos = s["elements"].index {|e| e["id"] == elt_id.split('|').last}
18
+ s = pos ? s["elements"][pos] : {"id" => elt_id, "elements" => []}
19
+ end
20
+ s["elements"]
21
+ end
22
+ end
23
+ end
24
+
25
+ def element(*ids)
26
+ unless ids.blank?
27
+ if ids.length == 1
28
+ section(ids.first)
29
+ else
30
+ elts = elements(*ids[0..-2]) || []
31
+ pos = elts.index {|e| e["id"] == ids.last}
32
+ pos ? elts[pos] : {"id" => ids.last}
33
+ end
34
+ end
35
+ end
36
+
37
+ def update_element(*ids, value)
38
+ unless ids.blank?
39
+ parent_section = find_or_init(*ids)["elements"]
40
+ if parent_section.any? {|elt| elt["id"] == value["id"]}
41
+ parent_section.map! {|elt| elt["id"] == value["id"] ? value : elt}
42
+ else
43
+ parent_section << value.to_h
44
+ end
45
+
46
+ value if save
47
+ end
48
+ end
49
+
50
+ def clone_element(*ids, element_id)
51
+ src_elt = find_or_init(*ids, element_id)
52
+ siblings = find_or_init(*ids)["elements"]
53
+ ref_index = siblings.index {|s| s["id"] == element_id}
54
+ new_elt = siblings[ref_index].deep_dup
55
+ new_elt["id"] = "cl#{Time.current.to_i}"
56
+ siblings.insert(ref_index + 1, new_elt)
57
+ save ? new_elt : nil
58
+ end
59
+
60
+ def delete_element(*ids, element_id)
61
+ src_elt = find_or_init(*ids, element_id)
62
+ siblings = find_or_init(*ids)["elements"]
63
+ ref_index = siblings.index {|s| s["id"] == element_id}
64
+ siblings.delete_at(ref_index)
65
+ save
66
+ end
67
+
68
+ def child_element(*ids, element_id)
69
+ siblings = elements(*ids)
70
+ parent_elt = siblings[siblings.index {|s| s["id"] == element_id}]
71
+ if parent_elt["elements"].blank?
72
+ parent_elt["elements"] = [{"id" => "cl#{Time.current.to_i}"}]
73
+ else
74
+ parent_elt["elements"] << {"id" => "cl#{Time.current.to_i}"}
75
+ end
76
+ save
77
+ end
78
+
79
+ def create_section(*ids, after, new_section)
80
+ new_section["id"] = "cs#{Time.current.to_i}"
81
+ if ids.length == 1
82
+ parent = sections
83
+ else
84
+ parent = find_or_init(*ids[0..-2])["elements"]
85
+ end
86
+ if new_section["template"].blank?
87
+ nil
88
+ else
89
+ template_defaults = (site_template.templates && site_template.templates[new_section["template"]]) ? site_template.templates[new_section["template"]] : {}
90
+ sec = template_defaults.merge(new_section)
91
+ ref_pos = parent.index {|s| s["id"] == ids.last}
92
+ parent.insert(after.to_s == 'true' ? ref_pos + 1 : ref_pos, sec)
93
+ sec if save
94
+ end
95
+ end
96
+
97
+ def delete_section(*ids)
98
+ if ids.length == 1
99
+ if sections.length == 1
100
+ nil
101
+ else
102
+ ref_index = sections.index {|s| s["id"] == ids.first}
103
+ sections.delete_at(ref_index)
104
+ save
105
+ end
106
+ else
107
+ parent = find_or_init(*ids[0..-2])
108
+ ref_index = parent["elements"].index {|s| s["id"] == ids.last}
109
+ parent["elements"].delete_at(ref_index)
110
+ save
111
+ end
112
+ end
113
+
114
+ def elt(siblings, elt_id, default_elt = nil)
115
+ pos = siblings.index {|e| e["id"] == elt_id}
116
+ pos ? siblings[pos] : default_elt
117
+ end
118
+
119
+ def find_or_init(*ids)
120
+ node = nil
121
+ siblings = sections
122
+ ids.each do |elt_id|
123
+ node = elt(siblings, elt_id)
124
+ if node.nil?
125
+ node = {"id" => elt_id, "elements" => []}
126
+ siblings << node
127
+ elsif node["elements"].nil?
128
+ node["elements"] = []
129
+ end
130
+ siblings = node["elements"]
131
+ end
132
+ node
133
+ end
134
+ end
135
135
  end
@@ -1,2 +1,2 @@
1
- class Sibu::StyleUploader < Shrine
1
+ class Sibu::StyleUploader < Shrine
2
2
  end
@@ -1,9 +1,9 @@
1
- module Sibu
2
- module UserConcern
3
- include ActiveSupport::Concern
4
-
5
- def for_user(usr)
6
- Rails.application.config.sibu[:multi_user] ? where(user_id: [nil, usr.id]) : all
7
- end
8
- end
1
+ module Sibu
2
+ module UserConcern
3
+ include ActiveSupport::Concern
4
+
5
+ def for_user(usr)
6
+ Rails.application.config.sibu[:multi_user] ? where(user_id: [nil, usr.id]) : all
7
+ end
8
+ end
9
9
  end
@@ -1,5 +1,5 @@
1
- module Sibu
2
- class ApplicationRecord < ActiveRecord::Base
3
- self.abstract_class = true
4
- end
5
- end
1
+ module Sibu
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -1,16 +1,16 @@
1
- module Sibu
2
- class Document < ApplicationRecord
3
- include DocumentUploader::Attachment.new(:file, cache: :documents_cache, store: :documents_store)
4
- extend Sibu::UserConcern
5
-
6
- validates_presence_of :file_data
7
-
8
- def metadata
9
- JSON.parse(file_data, symbolize_names: true)[:metadata]
10
- end
11
-
12
- def file_name
13
- metadata[:filename]
14
- end
15
- end
16
- end
1
+ module Sibu
2
+ class Document < ApplicationRecord
3
+ include DocumentUploader::Attachment.new(:file, cache: :documents_cache, store: :documents_store)
4
+ extend Sibu::UserConcern
5
+
6
+ validates_presence_of :file_data
7
+
8
+ def metadata
9
+ JSON.parse(file_data, symbolize_names: true)[:metadata]
10
+ end
11
+
12
+ def file_name
13
+ metadata[:filename]
14
+ end
15
+ end
16
+ end
@@ -1,77 +1,77 @@
1
- module Sibu
2
- class DynamicStyle
3
-
4
- attr_reader :site, :body, :env, :filename, :scss_file
5
-
6
- def initialize(site_id)
7
- @site = Sibu::Site.find(site_id)
8
- @filename = "#{site_id}_#{Time.current.to_i}"
9
- @scss_file = File.new(scss_file_path, 'w')
10
- @body = ERB.new(File.read(template_file_path)).result(binding)
11
- @env = Rails.application.assets
12
- end
13
-
14
- def compile
15
- find_or_create_scss
16
-
17
- begin
18
- scss_file.write generate_css
19
- scss_file.flush
20
- scss_file.close
21
- css_file_path = scss_file_path.gsub('scss', 'css')
22
- File.rename(scss_file_path, css_file_path)
23
- site.update(style: File.new(css_file_path))
24
- rescue Exception => ex
25
- Rails.logger.error(ex)
26
- ensure
27
- File.delete(scss_file_path) if scss_file_path && File.exist?(scss_file_path)
28
- File.delete(css_file_path) if css_file_path && File.exist?(css_file_path)
29
- end
30
- end
31
-
32
- def self.refresh_styles
33
- Sibu::Site.all.each do |s|
34
- Sibu::DynamicStyle.new(s.id).compile
35
- end
36
- end
37
-
38
- private
39
-
40
- def template_file_path
41
- @template_file_path ||= File.join(Rails.root, 'lib', 'assets', '_default_template.scss.erb')
42
- end
43
-
44
- def scss_tmpfile_path
45
- @scss_file_path ||= File.join(Rails.root, 'app', 'assets', 'stylesheets', 'templates')
46
- FileUtils.mkdir_p(@scss_file_path) unless File.exists?(@scss_file_path)
47
- @scss_file_path
48
- end
49
-
50
- def scss_file_path
51
- @scss_file_path ||= File.join(scss_tmpfile_path, "#{filename}.scss")
52
- end
53
-
54
- def find_or_create_scss
55
- File.open(scss_file_path, 'w') {|f| f.write(body)}
56
- end
57
-
58
- def generate_css
59
- Sass::Engine.new(asset_source, {
60
- syntax: :scss,
61
- cache: false,
62
- read_cache: false,
63
- style: :compressed
64
- }).render
65
- end
66
-
67
- def asset_source
68
- if env.find_asset(filename)
69
- env.find_asset(filename).source
70
- else
71
- uri = Sprockets::URIUtils.build_asset_uri(scss_file.path, type: "text/css")
72
- asset = Sprockets::UnloadedAsset.new(uri, env)
73
- env.load(asset.uri).source
74
- end
75
- end
76
- end
1
+ module Sibu
2
+ class DynamicStyle
3
+
4
+ attr_reader :site, :body, :env, :filename, :scss_file
5
+
6
+ def initialize(site_id)
7
+ @site = Sibu::Site.find(site_id)
8
+ @filename = "#{site_id}_#{Time.current.to_i}"
9
+ @scss_file = File.new(scss_file_path, 'w')
10
+ @body = ERB.new(File.read(template_file_path)).result(binding)
11
+ @env = Rails.application.assets
12
+ end
13
+
14
+ def compile
15
+ find_or_create_scss
16
+
17
+ begin
18
+ scss_file.write generate_css
19
+ scss_file.flush
20
+ scss_file.close
21
+ css_file_path = scss_file_path.gsub('scss', 'css')
22
+ File.rename(scss_file_path, css_file_path)
23
+ site.update(style: File.new(css_file_path))
24
+ rescue Exception => ex
25
+ Rails.logger.error(ex)
26
+ ensure
27
+ File.delete(scss_file_path) if scss_file_path && File.exist?(scss_file_path)
28
+ File.delete(css_file_path) if css_file_path && File.exist?(css_file_path)
29
+ end
30
+ end
31
+
32
+ def self.refresh_styles
33
+ Sibu::Site.all.each do |s|
34
+ Sibu::DynamicStyle.new(s.id).compile
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def template_file_path
41
+ @template_file_path ||= File.join(Rails.root, 'lib', 'assets', '_default_template.scss.erb')
42
+ end
43
+
44
+ def scss_tmpfile_path
45
+ @scss_file_path ||= File.join(Rails.root, 'app', 'assets', 'stylesheets', 'templates')
46
+ FileUtils.mkdir_p(@scss_file_path) unless File.exists?(@scss_file_path)
47
+ @scss_file_path
48
+ end
49
+
50
+ def scss_file_path
51
+ @scss_file_path ||= File.join(scss_tmpfile_path, "#{filename}.scss")
52
+ end
53
+
54
+ def find_or_create_scss
55
+ File.open(scss_file_path, 'w') {|f| f.write(body)}
56
+ end
57
+
58
+ def generate_css
59
+ Sass::Engine.new(asset_source, {
60
+ syntax: :scss,
61
+ cache: false,
62
+ read_cache: false,
63
+ style: :compressed
64
+ }).render
65
+ end
66
+
67
+ def asset_source
68
+ if env.find_asset(filename)
69
+ env.find_asset(filename).source
70
+ else
71
+ uri = Sprockets::URIUtils.build_asset_uri(scss_file.path, type: "text/css")
72
+ asset = Sprockets::UnloadedAsset.new(uri, env)
73
+ env.load(asset.uri).source
74
+ end
75
+ end
76
+ end
77
77
  end