Class: PolicyManager::ExporterHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/policy_manager/exporter/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ExporterHandler

Returns a new instance of ExporterHandler.



8
9
10
11
12
13
14
15
16
# File 'lib/policy_manager/exporter/handler.rb', line 8

def initialize(opts={})
  self.path = opts[:path]
  self.resource = if opts[:resource].is_a?(String)
    opts[:resource].constantize
  else
    opts[:resource]
  end
  self.after_zip = opts[:after_zip] if opts[:after_zip]
end

Instance Attribute Details

#after_zipObject

Returns the value of attribute after_zip.



6
7
8
# File 'lib/policy_manager/exporter/handler.rb', line 6

def after_zip
  @after_zip
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/policy_manager/exporter/handler.rb', line 6

def path
  @path
end

#resourceObject

Returns the value of attribute resource.



6
7
8
# File 'lib/policy_manager/exporter/handler.rb', line 6

def resource
  @resource
end

Instance Method Details

#base_dirObject



66
67
# File 'lib/policy_manager/exporter/handler.rb', line 66

def base_dir
end

#base_pathObject



18
19
20
# File 'lib/policy_manager/exporter/handler.rb', line 18

def base_path
  self.path.join resource.id.to_s
end

#clear!Object



42
43
44
45
# File 'lib/policy_manager/exporter/handler.rb', line 42

def clear!
  FileUtils.rm_rf(base_path)
  FileUtils.rm_rf(zip_path)
end

#create_sectionsObject



47
48
49
50
51
52
53
54
# File 'lib/policy_manager/exporter/handler.rb', line 47

def create_sections
  PolicyManager::Config.portability_rules.each do |rule|
    handle_render_for(rule)
  end

  render_index
  puts "FOLDER CREATED AT #{base_path}"
end

#generate_zipObject



141
142
143
144
145
146
# File 'lib/policy_manager/exporter/handler.rb', line 141

def generate_zip
  directory_to_zip = base_path.to_s
  output_file = zip_path.to_s
  zf = ZipGenerator.new(directory_to_zip, output_file)
  zf.write()
end

#handle_render_for(rule) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/policy_manager/exporter/handler.rb', line 56

def handle_render_for(rule)
  if rule.member
    render_member(rule)
  end

  if rule.collection
    render_collection(rule)
  end
end

#handle_zip_uploadObject



35
36
37
38
39
40
# File 'lib/policy_manager/exporter/handler.rb', line 35

def handle_zip_upload
  resource
  .portability_requests
  .find_by(state: "progress")
  .update_attributes(file_remote_url: zip_path)
end

#performObject



26
27
28
29
30
31
32
33
# File 'lib/policy_manager/exporter/handler.rb', line 26

def perform
  FileUtils.mkdir_p(base_path)
  create_sections
  generate_zip
  handle_zip_upload
  after_zip.call(zip_path, resource) if after_zip.is_a?(Proc)
  clear!
end

#render_collection(rule) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/policy_manager/exporter/handler.rb', line 93

def render_collection(rule)
  return unless resource.respond_to?(:portability_collection_for)
  o = resource.portability_collection_for(rule, 1)

  base_dir  = self.base_path.join(rule.name)
  FileUtils.mkdir_p(base_dir)

  (1..o.total_pages).to_a.each do |i|
    o = resource.portability_collection_for(rule, i)

    page_name = i
    folder_dir = page_name == 1 ? base_dir : base_dir.join(page_name.to_s)
    FileUtils.mkdir_p(folder_dir)
    resource_path = folder_dir.join("index.html")

    view = ExporterView.new({
      assigns: {collection: o} ,
      build_path: self.base_path,
      base_path: resource_path,
      template: rule.template,
      rule: rule
    }).save(resource_path)


    json = JsonExporterView.new({
      assigns: {collection: o},
      template: rule.json_template,
      folder: folder_dir
    }).save if rule.json_template.present?

    puts "saving at #{self.path.join rule.name}"
  end
end

#render_indexObject



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/policy_manager/exporter/handler.rb', line 127

def render_index
  resource_path = self.base_path.join("index.html")
  template = PolicyManager::Config.exporter.index_template
  view = ExporterView.new({
    assigns: {
      collection: PolicyManager::Config.portability_rules
    },
    build_path: self.base_path,
    base_path: resource_path,
    template: template
  }).save( resource_path )
  puts "saving at #{resource_path}"
end

#render_member(rule) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/policy_manager/exporter/handler.rb', line 69

def render_member(rule)
  return unless resource.respond_to?(:portability_member_for)
  o = resource.portability_member_for(rule)
  base_dir = self.base_path.join(rule.name)
  FileUtils.mkdir_p(base_dir)
  resource_path = base_dir.join("index.html")

  view = ExporterView.new({
    assigns: {member: o},
    build_path: self.base_path,
    base_path: resource_path,
    template: rule.template,
    rule: rule
  }).save(resource_path)

  puts "saving at #{self.path.join rule.name}"

  json = JsonExporterView.new({
    assigns: {member: o},
    template: rule.json_template,
    folder: base_dir
  }).save if rule.json_template.present?
end

#zip_pathObject



22
23
24
# File 'lib/policy_manager/exporter/handler.rb', line 22

def zip_path
  "#{base_path}-out.zip"
end