Module: RubberRing::Util

Included in:
AttachmentsController, CmsController
Defined in:
app/concerns/rubber_ring/util.rb

Class Method Summary collapse

Class Method Details

.get_attachment_directories(params) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/concerns/rubber_ring/util.rb', line 10

def Util.get_attachment_directories(params)
  controller  = params[:page_controller] || params[:controller]
  action      = params[:page_action]     || params[:action]
  locale      = params[:locale]          || I18n.default_locale.to_s

  image_src_dir = "upload/#{locale}/#{controller}/#{action}/images"
  file_src_dir  = "upload/#{locale}/#{controller}/#{action}/attachments"

  return Struct::AttachmentsDir.new(
    image_src_dir,
    file_src_dir,
    "public/#{image_src_dir}",
    "public/#{file_src_dir}"
  )
end

.get_options_from_params(params) ⇒ Object



49
50
51
52
53
54
55
56
# File 'app/concerns/rubber_ring/util.rb', line 49

def Util.get_options_from_params(params)
  {
    controller: params[:page_controller],
    action:     params[:page_action],
    locale:     params[:page_locale] || I18n.default_locale.to_s,
    content:    params[:content]
  }
end

.load_attachments_page(params) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/concerns/rubber_ring/util.rb', line 26

def Util.load_attachments_page(params)
  images = []
  attachments = []
  dir_config = get_attachment_directories(params)
  dirs_to_crawl = [dir_config.image_dir, dir_config.file_dir]

  dirs_to_crawl.each do |dir|
    if File.directory?(dir)
      Dir.foreach(dir) do |file|
        next if file == '.' or file == '..'

        if dir == dir_config.image_dir
          images << File.join(dir_config.image_src_dir, file)
        else
          attachments << File.join(dir_config.file_src_dir, file)
        end
      end
    end
  end

  return images, attachments
end

.move_file(source, destination) ⇒ Object



66
67
68
69
70
71
72
# File 'app/concerns/rubber_ring/util.rb', line 66

def Util.move_file(source, destination)
  unless File.directory?(destination)
    FileUtils.mkdir_p(destination)
  end

  FileUtils.mv(source, destination)
end

.save_file_to_fs(dir, name, file) ⇒ Object



58
59
60
61
62
63
64
# File 'app/concerns/rubber_ring/util.rb', line 58

def Util.save_file_to_fs(dir, name, file)
  unless File.directory?(dir)
    FileUtils.mkdir_p(dir)
  end

  File.open(File.join(dir, name), 'wb') { |f| f.write(file) }
end

.save_page_content(params) ⇒ Object



5
6
7
8
# File 'app/concerns/rubber_ring/util.rb', line 5

def Util.save_page_content(params)
  options = get_options_from_params(params)
  Page.save_or_update(options)
end