ecm_cms2 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f48acf799ce059fbf7de8cde688dc59a7a6e750f
4
- data.tar.gz: 50213ec2a5b3a04b3d68c1ffcfb6cb4e320c9f81
3
+ metadata.gz: a3afe34cc5af630dfb47930fc5ba8df17e0fcfb7
4
+ data.tar.gz: 604019f4cecd2c2c74edaf9050504f6bc5c50409
5
5
  SHA512:
6
- metadata.gz: a523585e6d1a8c559f6f6c33f1a91d12705be937b4625bf5c613f26b8d7e9d5d9556642b0954b66076c34a6f5d9edc638326ef6e608fec446fab2afaeb0096b3
7
- data.tar.gz: 3d9c68302699d0957a6a96a3316a1b0d37c1118590b0b44e60afd60cbaada57fa0b9d0b35c14ecee3c32c2341e5db281c3c32d7eed1b134cfac571d9e11745ef
6
+ metadata.gz: faa9101074e1f281cc61f4d15f17b84ca50b725b834df9e3cbb25cfb24823aeaaf826de7f4b891542d7fd62ffe7218cb273f0b34a1ce5dcae9826c05c7bd5119
7
+ data.tar.gz: b5882957087f8e09211f2b787bf7f430f5cdd75b8a507cd5e57881070ba2fc8b609eee5e00db7a092c45edd9fa1d309c7cf4d762c334408b3d837e2be5937174
@@ -0,0 +1,114 @@
1
+ module Ecm::Cms
2
+ class ImportPartialsService
3
+
4
+ class PartialInFileSystem
5
+ def initialize(filename, view_path)
6
+ @filename = filename
7
+ @view_path = view_path
8
+ end
9
+
10
+ def pathname
11
+ @pathname ||= File.dirname(relative_filename)
12
+ end
13
+
14
+ def basename
15
+ @basename ||= File.basename(relative_filename).split('.').first
16
+ end
17
+
18
+ def locale
19
+ locale = File.basename(relative_filename).split('.')[-3]
20
+ if I18n.available_locales.map(&:to_s).include?(locale)
21
+ @locale ||= locale
22
+ else
23
+ nil
24
+ end
25
+ end
26
+
27
+ def format
28
+ format = File.basename(relative_filename).split('.')[-2]
29
+ if Mime::SET.symbols.map(&:to_s).include?(format)
30
+ @format ||= format
31
+ else
32
+ nil
33
+ end
34
+ end
35
+
36
+ def handler
37
+ handler = File.basename(relative_filename).split('.').last
38
+ if ActionView::Template::Handlers.extensions.map(&:to_s).include?(handler)
39
+ @handler ||= handler
40
+ else
41
+ nil
42
+ end
43
+ end
44
+
45
+ def body
46
+ File.read(@filename)
47
+ end
48
+
49
+ def to_partial_attributes_hash
50
+ {
51
+ pathname: pathname,
52
+ basename: basename,
53
+ locale: locale,
54
+ format: format,
55
+ handler: handler,
56
+ body: body
57
+ }
58
+ end
59
+
60
+ def human
61
+ "#{relative_filename} (#{body.size} bytes)"
62
+ end
63
+
64
+ private
65
+
66
+ def relative_filename
67
+ @relative_filename ||= @filename.gsub(view_path.to_s, '')
68
+ end
69
+
70
+ def view_path
71
+ @view_path
72
+ end
73
+ end
74
+
75
+ def self.call(*args)
76
+ new(*args).do_work
77
+ end
78
+
79
+ def initialize(options = {})
80
+ options.reverse_merge!({ view_path: Rails.root.join(*%w(app views)) })
81
+ @view_path = options[:view_path]
82
+ end
83
+
84
+ def do_work
85
+ puts "Environment: #{Rails.env}"
86
+ @partials = load_partials
87
+ partials_count = @partials.size
88
+ puts "Processing #{partials_count} partials in #{view_path}:"
89
+ @partials.each_with_index do |partial, index|
90
+ puts " (#{index + 1}/#{partials_count}) #{partial.human}"
91
+ partial = Partial.new(partial.to_partial_attributes_hash)
92
+ if partial.save
93
+ puts " Created #{partial.human}"
94
+ else
95
+ puts " Could not create #{partial.human}. Errors: #{partial.errors.full_messages}"
96
+ end
97
+ end
98
+ end
99
+
100
+ private
101
+
102
+ def load_partials
103
+ load_partials_absolute.collect { |file| PartialInFileSystem.new(file, view_path) }
104
+ end
105
+
106
+ def load_partials_absolute
107
+ Dir.glob("#{view_path}/**/_*.*")
108
+ end
109
+
110
+ def view_path
111
+ @view_path
112
+ end
113
+ end
114
+ end
@@ -30,6 +30,10 @@ module Ecm
30
30
  module ClassMethods
31
31
  end
32
32
 
33
+ def human
34
+ "#{self.class.name}: #{path_and_filename}"
35
+ end
36
+
33
37
  def filename
34
38
  filename = basename.dup
35
39
  filename << ".#{locale}" if locale.present?
@@ -38,6 +42,10 @@ module Ecm
38
42
  filename
39
43
  end
40
44
 
45
+ def path_and_filename
46
+ "#{pathname}#{filename}"
47
+ end
48
+
41
49
  private
42
50
 
43
51
  def assert_trailing_slash_on_pathname
@@ -1,5 +1,5 @@
1
1
  module Ecm
2
2
  module Cms
3
- VERSION = "1.0.1"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
@@ -1,5 +1,10 @@
1
1
  namespace :ecm do
2
2
  namespace :cms do
3
+ desc 'Imports partials from app/views into the database'
4
+ task :import_partials, [:view_path] => [:environment] do |t, args|
5
+ args.with_defaults(:view_path => Rails.root.join(*%(app views)))
6
+ Ecm::Cms::ImportPartialsService.call(args)
7
+ end
3
8
  end
4
9
  end
5
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ecm_cms2
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Vasquez Angel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-16 00:00:00.000000000 Z
11
+ date: 2015-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -417,6 +417,7 @@ files:
417
417
  - app/models/ecm/cms/page/content_block.rb
418
418
  - app/models/ecm/cms/partial.rb
419
419
  - app/models/ecm/cms/template.rb
420
+ - app/services/ecm/cms/import_partials_service.rb
420
421
  - config/locales/ecm.cms.content_box.de.yml
421
422
  - config/locales/ecm.cms.content_box.en.yml
422
423
  - config/locales/ecm.cms.de.yml