-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from prey/action-view-renderer
Action view renderer & JSON support
- Loading branch information
Showing
22 changed files
with
262 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module PolicyManager | ||
class ExporterController < ActionController::Base | ||
|
||
include ExporterHelper | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module PolicyManager | ||
module ExporterHelper | ||
|
||
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}" | ||
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 | ||
|
||
private | ||
|
||
def save_image(remote_image, path) | ||
open(URI(path).path, 'wb') do |file| | ||
file << open(remote_image).read | ||
end | ||
end | ||
|
||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module PolicyManager | ||
class JsonLink | ||
|
||
def self.render(collection = nil) | ||
ActionController::Base.helpers.content_tag(:a, "Open as JSON", href: link(collection), target: '_blank') | ||
end | ||
|
||
private | ||
|
||
def self.link(collection) | ||
if collection.nil? || collection.current_page == 1 | ||
return "./data.json" | ||
else | ||
return "./../data.json" | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require "fileutils" | ||
|
||
module PolicyManager | ||
class JsonExporterView | ||
attr_accessor :template, :folder, :assigns | ||
|
||
def initialize(vars={}, options) | ||
self.folder = options[:folder] | ||
self.assigns = options[:assigns] | ||
@template = options.fetch(:template) #, self.class.template) | ||
return self | ||
end | ||
|
||
def save | ||
render_json | ||
end | ||
|
||
def save_json(file, data) | ||
File.open(file, "w") do |f| | ||
f.write(data) | ||
end | ||
end | ||
|
||
def render_json | ||
ac = PolicyManager::ExporterController.new() | ||
options = handled_template.merge!({assigns: self.assigns }) | ||
content = ac.render_to_string(options) | ||
save_json("#{folder}/data.json", content) | ||
end | ||
|
||
def handled_template | ||
begin | ||
if URI.parse(@template) | ||
return {template: @template} | ||
end | ||
rescue URI::InvalidURIError | ||
end | ||
|
||
if @template.is_a?(String) | ||
return {inline: @template} | ||
elsif @template.is_a?(Pathname) | ||
return {file: @template } | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.