dynamic_content 1.0.0 → 1.0.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2434c0cebf28a904c5b48ccda9b30d47ff1ef904
4
- data.tar.gz: 85a75f7304d490400b9d52b4cc13dec963b8ffef
3
+ metadata.gz: f202090c121b51f6689bc51e7dbb65d0550b272b
4
+ data.tar.gz: 71cfc0d7149a66743bb94c9f657df8051c47e1c4
5
5
  SHA512:
6
- metadata.gz: 5cc5c4ac273f16e5e99e7fc15eaa266688737acb548f7b426d6138d4ff71b1fdfb9eac564697e7445b5c44e2de8964fbbd1703936e13b08df2fd162208fde8c8
7
- data.tar.gz: 58138bdd8e5dfd17dde3e025016d852ef6746c63ec09850e4b23a65e75c7a2e217045b40d67e2c8d3ed444983dbb789d80e96fed1d0f6966e290c4c90675b38b
6
+ metadata.gz: 4864882df3786d1aa71cbf1975e855b87c4d3946a2fee58bd9b80be38fd96eb4f6e1c329825420714229b9da39b9b8d34e794092c95ef0f91359398613e67935
7
+ data.tar.gz: 88a5317f8c80f9d96bca727b6335cfe3e430ac297b9df0808fd260aa7d1376211e0dc8b38a4c845332f304e96f8b418879e84d456041aadbaa1c0728029c5536
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DynamicContentHelper
4
+
5
+ def c_image_tag section, content, *args
6
+ image, data, options = '', @dynamic_content, { alt: nil, size: '' }
7
+ options = options.merge(args.first) if args.count > 0
8
+
9
+ if data.nil?
10
+ raise DynamicContent::NoDataLoadedError, 'No Dynamic Content data loaded, try to include `before_action :load_dynamic_content` on your controller.'
11
+ end
12
+
13
+ if data.has_key?(section.to_sym) && data[section.to_sym].has_key?(content.to_sym)
14
+ data = data[section.to_sym][content.to_sym]
15
+
16
+ begin
17
+ options[:alt] = data[:alt] if options[:alt].blank?
18
+ image = data[:content]
19
+
20
+ if data[:data_type] == 'image' && !options[:size].blank? && data[:uid].split('.').last != 'svg'
21
+ image = Dragonfly.app.fetch(data[:uid]).thumb(options[:size]).url
22
+ end
23
+ rescue
24
+ Rails.logger.warn('WARNING: Error to load dynamic image '+section.to_s+':'+content.to_s+'.')
25
+ end
26
+ else
27
+ Rails.logger.warn('WARNING: Dynamic image data for '+section.to_s+':'+content.to_s+' was not found on database.')
28
+ end
29
+
30
+ options.delete(:size)
31
+ return options[:only_path] ? image : image_tag(image, options)
32
+ end
33
+
34
+ def c_image_path section, content, *args
35
+ options = { only_path: true }
36
+ options = options.merge(args.first) if args.count > 0
37
+
38
+ return c_image_tag(section, content, options)
39
+ end
40
+
41
+ def c section, content, *args
42
+ options = {default: '', preend: '', append: '', size: '', show: :content, only_numbers: false}
43
+ options = options.merge(args.first) if args.count > 0
44
+ data = @dynamic_content
45
+
46
+ # options[:append] += link_to_edit(section, content) if current_admin_user && !options[:only_numbers]
47
+
48
+ if data.nil?
49
+ raise DynamicContent::NoDataLoadedError, 'No Dynamic Content data loaded, try to include `before_action :load_dynamic_content` on your controller.'
50
+ end
51
+
52
+ if data.has_key?(section.to_sym) && data[section.to_sym].has_key?(content.to_sym)
53
+ result = data[section.to_sym][content.to_sym]
54
+ result = Dragonfly.app.fetch(result[:uid]).thumb(options[:size]).url if result.kind_of?(Hash) && result.has_key?(:uid) && !options[:size].blank?
55
+ result = result[options[:show].to_sym] if result.kind_of?(Hash)
56
+ result = options[:default] if result.blank?
57
+ result = result.scan(/\d/).join if options[:only_numbers]
58
+ else
59
+ Rails.logger.warn('WARNING: Dynamic content data for '+section.to_s+':'+content.to_s+' was not found on database.')
60
+ result = options[:default]
61
+ end
62
+
63
+ return result.kind_of?(String) ? (result.blank? ? '' : (options[:preend].html_safe + result + options[:append].html_safe)) : result
64
+ end
65
+
66
+ def link_to_edit section, content
67
+ if @page.has_key?(section.to_sym) && @page[section.to_sym].has_key?(content.to_sym)
68
+ id = @page[:id]
69
+ elsif @application.has_key?(section.to_sym) && @application[section.to_sym].has_key?(content.to_sym)
70
+ id = @application[:id]
71
+ else
72
+ return ''
73
+ end
74
+
75
+ return (' ' + link_to('', edit_admin_page_path(id)+'#'+section.to_s+'-'+content.to_s, class: 'admin-edit', data: {section: section, content: content, page: id}, title: 'Editar', target: '_blank'))
76
+ end
77
+
78
+ end
@@ -0,0 +1,129 @@
1
+ module DynamicContent
2
+ class Content < ActiveRecord::Base
3
+ self.table_name = 'dynamic_content_contents'
4
+
5
+ belongs_to :section
6
+
7
+ serialize :data_options, JSON
8
+
9
+ default_scope { order('dynamic_content_contents.order ASC') }
10
+
11
+ dragonfly_accessor :file do
12
+ after_assign{ |a| a.thumb!('1000x1000^') if ::MIME::Types.type_for(self.file_name).first.try(:content_type).to_s.start_with? 'image' }
13
+ end
14
+
15
+ # hash key: dynamic content data type
16
+ # field: where content is stored on database
17
+ # as: formtastic input type
18
+ # wrapper_class: css class of element wrapper, like li tag on formtastic
19
+ # input_class: css class of input element
20
+ INPUT_TYPES = {
21
+ default: { field: :content, as: :string, wrapper_class: 'string', input_class: '' },
22
+ select: { as: :select },
23
+ boolean: { as: :boolean },
24
+ text: { as: :text },
25
+ markdown: { as: :text },
26
+ editor: { as: :text, input_class: 'complete-editor' },
27
+ embed: {},
28
+ uploader: { field: :file, as: :file },
29
+ image: { field: :file },
30
+ date: { field: :date, as: :date_select },
31
+ time: { field: :date, as: :time_select },
32
+ datetime: { field: :date, as: :datetime_select },
33
+ video: { as: :url },
34
+ currency: { as: :number },
35
+ hidden: { as: :hidden }
36
+ }
37
+
38
+ INPUT_MODIFIERS = {
39
+ markdown: { string: { as: :string } },
40
+ editor: { simple_editor: { input_class: 'simple-editor' }, 'list-editor': { input_class: 'list-editor' } }
41
+ }
42
+
43
+ before_validation do
44
+ case self.data_type
45
+ when 'editor'
46
+ if self.type_options == 'list-editor'
47
+ self.content = ActionController::Base.helpers.sanitize(self.content.to_s, tags: %w(ul li strong em u span), attributes: [])
48
+ self.content = self.content[self.content.index('<ul>'), self.content.index('</ul>')] unless self.content.to_s['<ul>'].blank?
49
+ end
50
+ end
51
+ end
52
+
53
+ def get_type_options
54
+ return @type_options unless @type_options.nil?
55
+
56
+ @type_options = INPUT_TYPES[:default].dup
57
+ current_type = self.data_type.to_sym
58
+
59
+ @type_options.merge!(INPUT_TYPES[current_type]) if INPUT_TYPES.include?(current_type)
60
+
61
+ if INPUT_MODIFIERS.include?(current_type) && INPUT_MODIFIERS[current_type].include?(self.type_options.to_sym)
62
+ @type_options.merge!(INPUT_MODIFIERS[current_type][self.type_options.to_sym])
63
+ end
64
+
65
+ @type_options[:wrapper_class] += (self.has_divisor ? ' has-divisor' : '')
66
+
67
+ return @type_options
68
+ end
69
+
70
+ def formtastic_compatible?
71
+ not_compatible = %w(image uploader embed)
72
+
73
+ return !self.has_caption && !not_compatible.include?(self.data_type.to_s)
74
+ end
75
+
76
+ def get_content *args
77
+ case self.data_type
78
+ when 'select'
79
+ result = self.content.to_i
80
+ when 'boolean'
81
+ result = self.content.to_i == 1
82
+ when 'currency'
83
+ # result = self.content.gsub(',', '.').to_f
84
+ result = ActionController::Base.helpers.number_to_currency(self.content.gsub(',', '.').to_f)
85
+ when 'editor'
86
+ result = self.content.html_safe
87
+ when 'markdown'
88
+ markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: false, filter_html: true, no_styles: true, no_images: true, highlight: true)
89
+ result = markdown.render(self.content).gsub('<p>', '').gsub('</p>', '').html_safe
90
+ self.caption = markdown.render(self.caption).gsub('<p>', '').gsub('</p>', '') if self.has_caption
91
+ when 'embed'
92
+ result = self.content.html_safe
93
+ when 'uploader'
94
+ result = self.file.url
95
+ when 'image'
96
+ result = {content: (self.file ? self.file.url : ''), alt: self.content, uid: self.file_uid}
97
+ when 'date'
98
+ result = self.date.blank? ? nil : self.date.to_date
99
+ when 'datetime'
100
+ result = self.date
101
+ when 'video'
102
+ result = {content: '', provider: :none}
103
+
104
+ unless self.content.blank?
105
+ parsed_url = self.content.gsub('https', 'http').match(/http:\/\/(?:www\.)?(vimeo|youtube)\.com\/(?:watch\?v=)?(.*?)(?:\z|$|&)/)
106
+
107
+ if !parsed_url.nil? && ['youtube', 'vimeo'].include?(parsed_url[1])
108
+ result = {
109
+ content: parsed_url[2],
110
+ provider: parsed_url[1].to_sym,
111
+ link: (parsed_url[1] == 'youtube' ? "http://youtu.be/#{parsed_url[2]}" : "https://vimeo.com/#{parsed_url[2]}"),
112
+ embed: (parsed_url[1] == 'youtube' ? "https://www.youtube.com/embed/#{parsed_url[2]}" : "https://player.vimeo.com/video/#{parsed_url[2]}?color=ffffff")
113
+ }
114
+ end
115
+ end
116
+ else
117
+ result = self.content
118
+ end
119
+
120
+ result[:data_type] = self.data_type if result.kind_of?(Hash)
121
+
122
+ if self.has_caption
123
+ return {caption: self.caption.html_safe, content: result}
124
+ else
125
+ return result
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,50 @@
1
+ module DynamicContent
2
+ class Page < ActiveRecord::Base
3
+ self.table_name = 'dynamic_content_pages'
4
+
5
+ has_many :sections, dependent: :destroy
6
+ accepts_nested_attributes_for :sections, allow_destroy: false
7
+
8
+ scope :get_page, -> (page){ includes(:sections).find_by_slug(page) }
9
+
10
+ after_commit :flush_cache
11
+
12
+ serialize :keywords, TagSerializer.new
13
+
14
+ def self.load page
15
+ current = self.includes(:sections).find_by_slug(page)
16
+ return {} if current.nil?
17
+
18
+ results = {
19
+ id: current.id,
20
+ title: current.title,
21
+ keywords: current.keywords.try(:join, ', '),
22
+ description: current.description
23
+ }
24
+
25
+ sections_content = current.sections#.where(on_application: false)
26
+ sections_content = sections_content + Section.where(on_application: true) if page == 'application'
27
+
28
+ sections_content.each do |section|
29
+ results[section.slug.to_sym] = {
30
+ name: section.name
31
+ }
32
+ section.contents.each do |content|
33
+ results[section.slug.to_sym][content.slug.to_sym] = content.get_content
34
+ end
35
+ end
36
+
37
+ return results
38
+ end
39
+
40
+ def self.cached_for page
41
+ Rails.cache.fetch([name, ("cached_page_#{page}".downcase)]) { Page.load(page) }
42
+ end
43
+
44
+ def flush_cache
45
+ Rails.cache.delete([self.class.name, ("cached_page_#{self.slug}".downcase)])
46
+ Rails.cache.delete([self.class.name, ("cached_page_application".downcase)])
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,10 @@
1
+ module DynamicContent
2
+ class Section < ActiveRecord::Base
3
+ self.table_name = 'dynamic_content_sections'
4
+
5
+ belongs_to :page
6
+ has_many :contents, dependent: :destroy
7
+
8
+ accepts_nested_attributes_for :contents, allow_destroy: false
9
+ end
10
+ end
@@ -8,6 +8,9 @@ module DynamicContent
8
8
  class DependencyError < ErrorLoading
9
9
  end
10
10
 
11
+ class NoDataLoadedError < ErrorLoading
12
+ end
13
+
11
14
  class NoStructureFileError < KeyError
12
15
  end
13
16
 
@@ -1,3 +1,3 @@
1
1
  module DynamicContent
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -7,7 +7,6 @@ require 'mime-types'
7
7
  module DynamicContent
8
8
  autoload :VERSION, 'dynamic_content/version'
9
9
  autoload :TagSerializer, 'dynamic_content/tag_serializer'
10
- autoload :Models, 'dynamic_content/models'
11
10
  autoload :Processor, 'dynamic_content/processor'
12
11
 
13
12
  class << self
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamic_content
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hugo Demiglio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-24 00:00:00.000000000 Z
11
+ date: 2018-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dragonfly
@@ -46,6 +46,10 @@ executables: []
46
46
  extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
+ - app/helpers/dynamic_content_helper.rb
50
+ - app/models/dynamic_content/content.rb
51
+ - app/models/dynamic_content/page.rb
52
+ - app/models/dynamic_content/section.rb
49
53
  - lib/dynamic_content.rb
50
54
  - lib/dynamic_content/engine.rb
51
55
  - lib/dynamic_content/error.rb
@@ -83,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
87
  version: '0'
84
88
  requirements: []
85
89
  rubyforge_project:
86
- rubygems_version: 2.6.14
90
+ rubygems_version: 2.5.1
87
91
  signing_key:
88
92
  specification_version: 4
89
93
  summary: Editable content on dynamic fields.