lib/policy_manager/exporter/view.rb in gdpr_rails-0.2.4 vs lib/policy_manager/exporter/view.rb in gdpr_rails-0.3.0
- old
+ new
@@ -12,26 +12,23 @@
include ERB::Util
include ActionView::Helpers
include WillPaginate::ViewHelpers #if defined?(WillPaginate)
include WillPaginate::ActionView #if defined?(WillPaginate)
- attr_accessor :template, :base_path
+ attr_accessor :template, :base_path, :assigns
def self.template
"Welcome, <%= @name %>"
end
- def initialize(vars={}, options={}, date=Time.now)
- # collection or member, or wathever!?
- vars.each{|k, v| self.instance_variable_set("@#{k}", v)}
+ def initialize(options={}, date=Time.now)
@base_path = options[:base_path]
@build_path = options[:build_path]
-
+ self.assigns = options[:assigns]
index_path
-
@template = options.fetch(:template, self.class.template)
-
+ return self
end
def index_path
path = @base_path.to_s.gsub(@build_path.to_s, "")
len = path.split("/").size
@@ -45,63 +42,42 @@
else
@index_path = "../../"
end
end
- def image_tag(remote_image, opts={})
- begin
- basename = File.basename(remote_image)
- id = opts[:id] || SecureRandom.hex(10)
- composed_name = [id, basename].compact.join("-")
- path = "#{File.dirname(base_path)}/#{composed_name}"
- self.save_image(remote_image, path)
- tag(:img, {src: "./#{id}-#{File.basename(URI(remote_image).path)}" }.merge(opts))
- rescue => e
- Config.error_notifier_method(e)
- content_tag(:p, "broken image")
- end
- end
-
- def save_image(remote_image, path)
- open(URI(path).path, 'wb') do |file|
- file << open(remote_image).read
- end
- end
-
def render()
- #template_layout = Tilt::ERBTemplate.new {PolicyManager::Config.exporter.layout}
context = self
- #template_layout.render {
- # view = Tilt::ERBTemplate.new{handled_template}
- # view.render(context)
- #}
-
- render_with_layout()
+ ac = PolicyManager::ExporterController.new()
+ options = handled_template.merge!({
+ assigns: self.assigns.merge!({
+ base_path: base_path,
+ build_path: @build_path,
+ index_path: index_path
+ }),
+ layout: PolicyManager::Config.exporter.layout
+ })
+ ac.render_to_string(options)
end
- def render_with_layout(context = self)
- render_layout do
- ERB.new(handled_template).result(binding)
- end
- end
-
- def render_layout
- layout = PolicyManager::Config.exporter.layout #File.read('views/layouts/app.html.erb')
- ERB.new(layout).result(binding)
- end
-
-
def save(file)
File.open(file, "w+") do |f|
f.write(render)
end
end
+ # TODO: method duplicated from json
def handled_template
+ begin
+ if URI.parse(@template)
+ return {template: @template}
+ end
+ rescue URI::InvalidURIError
+ end
+
if @template.is_a?(String)
- @template
+ return {inline: @template}
elsif @template.is_a?(Pathname)
- File.open(@template).read
+ return {file: @template }
end
end
end
end
\ No newline at end of file