Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactored converting to and from report names

  • Loading branch information...
commit 9b3290ab2d6f770b5e31e8d17b9bb5788f9c085e 1 parent 29074ff
@adamhunter adamhunter authored
View
4 app/controllers/dossier/reports_controller.rb
@@ -7,7 +7,7 @@ def show
respond_to do |format|
format.html do
begin
- render template: "dossier/reports/#{report.view}", locals: {report: report}
+ render template: "dossier/reports/#{report_class.report_name}", locals: {report: report}
rescue ActionView::MissingTemplate => e
render template: 'dossier/reports/show', locals: {report: report}
end
@@ -32,7 +32,7 @@ def show
private
def report_class
- "#{params[:report].split('_').map(&:capitalize).join}Report".constantize
+ Dossier.name_to_class(params[:report])
end
def set_content_disposition!
View
15 lib/dossier.rb
@@ -2,21 +2,30 @@
require "dossier/version"
module Dossier
+ extend self
- def self.configuration
+ def configuration
@configuration || configure
end
- def self.configure
+ def configure
@configuration = Configuration.new
yield(@configuration) if block_given?
@configuration
end
- def self.client
+ def client
configuration.client
end
+ def class_to_name(klass)
+ klass.name.underscore[0..-8]
+ end
+
+ def name_to_class(name)
+ "#{name.split('_').map(&:capitalize).join}Report".constantize
+ end
+
class ExecuteError < StandardError; end
end
View
8 lib/dossier/report.rb
@@ -7,6 +7,10 @@ class Report
attr_reader :options
+ def self.report_name
+ Dossier.class_to_name(self)
+ end
+
def initialize(options = {})
@options = options.dup.with_indifferent_access
end
@@ -34,10 +38,6 @@ def run
tap { execute }
end
- def view
- self.class.name.sub(/Report\Z/, '').underscore
- end
-
def formatter
Dossier::Formatter
end
View
10 spec/dossier/report_spec.rb
@@ -4,6 +4,10 @@
let(:report) { TestReport.new(:foo => 'bar') }
+ it "has a report name" do
+ TestReport.report_name.should eq('test')
+ end
+
describe "report instances" do
it "takes options when initializing" do
report = TestReport.new(:foo => 'bar')
@@ -56,12 +60,6 @@ def sql; ''; end
end
end
- describe "view" do
- it "will infer its view name from the class name" do
- EmployeeReport.new.view.should eq("employee")
- end
- end
-
end
end
View
15 spec/dossier_spec.rb
@@ -5,7 +5,7 @@
Dossier.should be_a(Module)
end
- it "is configuraable" do
+ it "is configurable" do
Dossier.configure
Dossier.configuration.should_not be_nil
end
@@ -28,4 +28,17 @@
Dossier.configuration.should_receive(:client)
Dossier.client
end
+
+ describe "report naming" do
+ let(:klass) { HelloMyFriendsReport }
+ let(:name) { 'hello_my_friends' }
+
+ it "converts a report class to a report name" do
+ expect(Dossier.class_to_name(klass)).to eq(name)
+ end
+
+ it "converting a report name to a report class" do
+ expect(Dossier.name_to_class(name)).to eq(klass)
+ end
+ end
end
View
5 spec/support/reports/hello_my_friends_report.rb
@@ -0,0 +1,5 @@
+class HelloMyFriendsReport < Dossier::Report
+ def sql
+ "select * from employees where friends = true and me = :self group by this is not a real query"
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.