Class: Cms::Component

Inherits:
Object
  • Object
show all
Defined in:
app/models/cms/component.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, path = nil) ⇒ Component

Returns a new instance of Component.



6
7
8
9
# File 'app/models/cms/component.rb', line 6

def initialize(context, path = nil)
  @context = context
  @path = self.class.clean_path(path) if path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'app/models/cms/component.rb', line 4

def path
  @path
end

Class Method Details

.base_path(context) ⇒ Object

base public path for components



12
13
14
# File 'app/models/cms/component.rb', line 12

def self.base_path(context)
  Pathname.new(File.join('cms', context.object ? File.join('components', context.object.id.to_s) : 'components'))
end

.component_path(context, path) ⇒ Object

the path for just the current component file as it existed in the original zip file returns empty if the path isn’t in the main component directory which occurs if the zip contained relative references path: the full path of the component on the file system



24
25
26
# File 'app/models/cms/component.rb', line 24

def self.component_path(context, path)
  path.to_s.include?(full_path(context).to_s) ? path.sub(full_path(context).to_s + "/", '') : ''
end

.editable?(file) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'app/models/cms/component.rb', line 80

def self.editable?(file)
  file.present? && Cms.editable_component_exts.include?(File.extname(file).downcase)
end

.expand(context, file) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/cms/component.rb', line 36

def self.expand(context, file)
  Zip::ZipFile.open(file) do |zip_file|
    zip_file.each do |f|
      f_path = full_path(context).join(f.name)
      if valid_ext?(f_path) && component_path(context, f_path).present?
        FileUtils.mkdir_p f_path.dirname
        FileUtils.rm_rf(f_path.to_s) if f_path.exist?
        zip_file.extract(f, f_path.to_s)
      end
    end
  end
end

.files(path) ⇒ Object



28
29
30
# File 'app/models/cms/component.rb', line 28

def self.files(path)
  Dir[File.expand_path(path) + "/*"]
end

.full_path(context) ⇒ Object

full component path on the local system



17
18
19
# File 'app/models/cms/component.rb', line 17

def self.full_path(context)
  Rails.root.join 'public', base_path(context)
end

.valid_ext?(file) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/models/cms/component.rb', line 32

def self.valid_ext?(file)
  Cms.valid_component_exts.include?(File.extname(file).downcase)
end

Instance Method Details

#deleteObject



71
72
73
74
75
76
77
78
# File 'app/models/cms/component.rb', line 71

def delete
  return false if @path.blank?

  fname = self.class.full_path(@context).join(@path)
  File.exist?(fname).tap do |exist|
    FileUtils.rm_rf fname if exist
  end
end

#readObject



49
50
51
52
53
54
55
56
57
58
# File 'app/models/cms/component.rb', line 49

def read
  return '' if @path.blank? || !self.class.editable?(@path)

  fname = self.class.full_path(@context).join(@path)
  if File.exist?(fname)
    File.open(fname).read
  else
    ''
  end
end

#write(content) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'app/models/cms/component.rb', line 60

def write(content)
  return false if content.blank? || @path.blank? || !self.class.editable?(@path)

  fname = self.class.full_path(@context).join(@path)
  File.exist?(fname).tap do |exist|
    File.open(fname, 'w') do |f|
      f.puts content
    end if exist
  end
end