Skip to content

Commit

Permalink
Merge e4bd509 into e1b3c16
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhunter committed Aug 20, 2015
2 parents e1b3c16 + e4bd509 commit abafa2f
Show file tree
Hide file tree
Showing 47 changed files with 358 additions and 158 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -6,7 +6,6 @@ Gemfile.lock
log/*.log
pkg/
coverage/*
spec/dummy/config/database.yml
spec/dummy/db/*.sqlite3
spec/dummy/log/*.log
spec/dummy/tmp/
Expand Down
16 changes: 12 additions & 4 deletions .travis.yml
Expand Up @@ -3,13 +3,21 @@ rvm:
- "1.9.3"
- "2.0.0"
- "2.1.2"
- "2.2.2"
env:
- "RAILS_VERSION=3.2.17"
- "RAILS_VERSION=4.0.4"
- "RAILS_VERSION=4.1.0"
script: bundle exec rspec spec
- "RAILS_VERSION=3.2.22"
- "RAILS_VERSION=4.0.13"
- "RAILS_VERSION=4.1.12"
- "RAILS_VERSION=4.2.3"
script:
- DOSSIER_DB=sqlite3 bundle exec rspec
- DOSSIER_DB=mysql2 bundle exec rspec
- DOSSIER_DB=postgresql bundle exec rspec
before_script:
- mysql -e 'create database dossier_test;'
- psql -c 'create database dossier_test;' -U postgres
- cp spec/dummy/config/database.yml.travis spec/dummy/config/database.yml
- cp spec/fixtures/db/mysql2.yml.travis spec/fixtures/db/mysql2.yml
- cp spec/fixtures/db/sqlite3.yml.travis spec/fixtures/db/sqlite3.yml
- cp spec/fixtures/db/postgresql.yml.travis spec/fixtures/db/postgresql.yml
sudo: false # upgrade travis ci infrastructure http://bit.ly/1J6D4W9
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,10 +3,14 @@
Dossier does its best to use [semantic versioning](http://semver.org).

## Unreleased
- Reports have a `display_column?(name)` method that can be overriden to
determine if a column should be displayed.
- Rails v4.2.X support
- headers will now be formatted without calling `format_header` in the view, that will be called when accessing them (I'm not sure if this may cause backwards incompatible changes with custom views. I don't *think* so.
- introduced `format_column(column, value)` as a default formatter that can be implemented as a fall back if a specific format method does not exist
- Add license to gemspec, thanks to notice from [Benjamin Fleischer](https://github.com/bf4) - see [his blog post](http://www.benjaminfleischer.com/2013/07/12/make-the-world-a-better-place-put-a-license-in-your-gemspec/)
- Output files now have a sortable date/time stamp by default. Eg, "foo-report_2014-10-02_09-12-24-EDT.csv". This can still be customized by defining a report class's `filename` method.
- Add CSS classes to report `<table>` elements to facilitate styling.

## v2.8.0
- Support namespaces for report names (`cats/are/super_fun` => `Cats::Are::SuperRunReport`
Expand Down
7 changes: 6 additions & 1 deletion Gemfile
Expand Up @@ -2,7 +2,7 @@ source "https://rubygems.org"

gemspec

RAILS_VERSION = ENV.fetch('RAILS_VERSION', '4.1.1')
RAILS_VERSION = ENV.fetch('RAILS_VERSION', '4.2.3')
gem "activesupport", RAILS_VERSION
gem "actionpack", RAILS_VERSION
gem "actionmailer", RAILS_VERSION
Expand All @@ -12,4 +12,9 @@ gem "activerecord", RAILS_VERSION
# gems used by the dummy application
gem "jquery-rails"
gem "mysql2"
gem "pg"
gem 'coveralls', require: false

# for rspec-rails on rails >= 4.2.0
# gem 'test-unit' if Gem::Version.new(RAILS_VERSION) >= Gem::Version.new('4.2.0')
gem 'test-unit' if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.2.0')
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -181,9 +181,13 @@ It's up to you to use these options in generating your SQL query.

However, Dossier does support one URL parameter natively: if you supply a `footer` parameter with an integer value, the last N rows will be accesible via `report.results.footers` instead of `report.results.body`. The built-in `show` view renders those rows inside an HTML footer. This is an easy way to display a totals row or something similar.

## Styling

The default report views use a `<table class="dossier report">` for easy CSS styling.

## Additional View Customization

To further customize your results view, run the the generator provided. The default will provide 'app/views/dossier/reports/show'.
To further customize your results view, run the generator provided. The default will provide 'app/views/dossier/reports/show'.

```ruby
rails generate dossier:views
Expand Down
17 changes: 12 additions & 5 deletions Rakefile
@@ -1,13 +1,20 @@
#!/usr/bin/env rake

begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end

Bundler::GemHelper.install_tasks
require 'rdoc/task'

require 'rails/all'
require 'dummy/application/tasks'
RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'Dossier'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('lib/**/*.rb')
end

APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
load 'rails/tasks/engine.rake'

Bundler::GemHelper.install_tasks
2 changes: 1 addition & 1 deletion app/controllers/dossier/reports_controller.rb
Expand Up @@ -17,7 +17,7 @@ def multi
private

def report_class
Dossier::Naming.name_to_class(params[:report])
Dossier::Model.name_to_class(params[:report])
end

def report
Expand Down
2 changes: 1 addition & 1 deletion app/views/dossier/reports/show.html.haml
Expand Up @@ -4,7 +4,7 @@

= render_options(report)

%table
%table.dossier.report
%thead
%tr
- report.results.headers.each do |header|
Expand Down
22 changes: 11 additions & 11 deletions dossier.gemspec
Expand Up @@ -7,11 +7,11 @@ require "dossier/version"
Gem::Specification.new do |s|
s.name = "dossier"
s.version = Dossier::VERSION
s.authors = ["Adam Hunter", "Nathan Long", "Rodney Turnham"]
s.email = ["adamhunter@me.com", "nathanmlong@gmail.com", "rodney.turnham@tma1.com"]
s.authors = ["TMA IT"]
s.email = ["developer@tma1.com"]
s.summary = "SQL based report generation."
s.description = "Easy SQL based report generation with the ability to accept request parameters and render multiple formats."
s.homepage = "https://github.com/adamhunter/dossier"
s.homepage = "https://github.com/tma1/dossier"
s.license = 'MIT'

s.files = Dir["{app,config,db,lib}/**/*"] + %w[MIT-LICENSE Rakefile README.md]
Expand All @@ -23,13 +23,13 @@ Gem::Specification.new do |s|
s.add_dependency "actionmailer", ">= 3.2"
s.add_dependency "railties", ">= 3.2"
s.add_dependency "haml", ">= 3.1"
s.add_dependency "responders", ">= 1.1"

s.add_development_dependency "activerecord", ">= 3.2"
s.add_development_dependency "sqlite3", ">= 1.3.6"
s.add_development_dependency "pry", ">= 0.9.10"
s.add_development_dependency "rspec-rails", ">= 2.14.1"
s.add_development_dependency "genspec", "~> 0.2.7"
s.add_development_dependency "capybara", "~> 2.1.0"
s.add_development_dependency "simplecov", "~> 0.7.1"
s.add_development_dependency "dummy-application"
s.add_development_dependency "activerecord", ">= 3.2"
s.add_development_dependency "sqlite3", ">= 1.3.6"
s.add_development_dependency "pry", ">= 0.10.1"
s.add_development_dependency "rspec-rails", ">= 3.3.3"
s.add_development_dependency "generator_spec", "~> 0.9.3"
s.add_development_dependency "capybara", "~> 2.4.4"
s.add_development_dependency "simplecov", "~> 0.10.0"
end
2 changes: 1 addition & 1 deletion lib/dossier.rb
@@ -1,5 +1,5 @@
require "dossier/engine"
require "dossier/naming"
require "dossier/model"
require "dossier/view_context_with_report_formatter"
require "dossier/version"

Expand Down
5 changes: 4 additions & 1 deletion lib/dossier/client.rb
Expand Up @@ -7,7 +7,10 @@ class Client

def initialize(options)
self.options = options.symbolize_keys
self.adapter = dossier_adapter.new(self.options.except(:dossier_adapter))
end

def adapter
@adapter ||= dossier_adapter.new(self.options.except(:dossier_adapter))
end

def dossier_adapter
Expand Down
15 changes: 10 additions & 5 deletions lib/dossier/configuration.rb
Expand Up @@ -4,6 +4,8 @@
module Dossier
class Configuration

DB_KEY = 'DATABASE_URL'.freeze

attr_accessor :config_path, :client

def initialize
Expand All @@ -12,25 +14,28 @@ def initialize
end

def connection_options
yaml_config.merge(dburl_config || {})
yaml_config.merge(dburl_config || {}).presence || raise_empty_conn_config
end

def yaml_config
YAML.load(ERB.new(File.read(@config_path)).result)[Rails.env].symbolize_keys
YAML.load(ERB.new(File.read(config_path)).result)[Rails.env].symbolize_keys
rescue Errno::ENOENT
{}
end

def dburl_config
Dossier::ConnectionUrl.new.to_hash if ENV.has_key? "DATABASE_URL"
Dossier::ConnectionUrl.new.to_hash if ENV.has_key? DB_KEY
end

private

def setup_client!
@client = Dossier::Client.new(connection_options)
end

rescue Errno::ENOENT => e
def raise_empty_conn_config
raise ConfigurationMissingError.new(
"#{e.message}. #{@config_path} must exist for Dossier to connect to the database."
"Your connection options are blank, you are missing both #{config_path} and ENV['#{DB_KEY}']"
)
end

Expand Down
12 changes: 10 additions & 2 deletions lib/dossier/naming.rb → lib/dossier/model.rb
@@ -1,5 +1,5 @@
module Dossier
module Naming
module Model

# not using ActiveSupport::Concern because ClassMethods
# must be extended after ActiveModel::Naming
Expand Down Expand Up @@ -28,11 +28,19 @@ def to_s
report_name
end

def to_model
self
end

def persisted?
true
end

delegate :report_name, :formatted_title, to: "self.class"

module ClassMethods
def report_name
Dossier::Naming.class_to_name(self)
Dossier::Model.class_to_name(self)
end

def formatted_title
Expand Down
2 changes: 1 addition & 1 deletion lib/dossier/multi_report.rb
@@ -1,5 +1,5 @@
class Dossier::MultiReport
include Dossier::Naming
include Dossier::Model

attr_accessor :options

Expand Down
2 changes: 1 addition & 1 deletion lib/dossier/query.rb
Expand Up @@ -15,7 +15,7 @@ def to_s
private

def compile
string.gsub(/\w*\:[a-z]{1}\w*/) { |match| escape(report.public_send(match[1..-1])) }
string.gsub(/\w*(?<!:):(?!:)[a-z]{1}\w*/) { |match| escape(report.public_send(match[1..-1])) }
end

def escape(value)
Expand Down
19 changes: 9 additions & 10 deletions lib/dossier/report.rb
@@ -1,6 +1,6 @@
module Dossier
class Report
include Dossier::Naming
include Dossier::Model
include ActiveSupport::Callbacks

define_callbacks :build_query, :execute
Expand Down Expand Up @@ -38,13 +38,13 @@ def query
end

def results
execute unless defined?(@results)
@results
execute unless query_results
@results ||= Result::Formatted.new(query_results, self)
end

def raw_results
execute unless defined?(@raw_results)
@raw_results
execute unless query_results
@raw_results ||= Result::Unformatted.new(query_results, self)
end

def run
Expand Down Expand Up @@ -82,15 +82,14 @@ def build_query
def execute
build_query
run_callbacks :execute do
self.results = dossier_client.execute(query, self.class.name)
self.query_results = dossier_client.execute(query, self.class.name)
end
end

def results=(results)
results.freeze
@raw_results = Result::Unformatted.new(results, self)
@results = Result::Formatted.new(results, self)
def query_results=(query_results)
@query_results = query_results.freeze
end
attr_reader :query_results

end
end
2 changes: 2 additions & 0 deletions lib/dossier/responder.rb
@@ -1,3 +1,5 @@
require 'responders' unless defined? ::ActionController::Responder

module Dossier
class Responder < ::ActionController::Responder
alias :report :resource
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/dossier/views/templates/show.html.haml
Expand Up @@ -4,7 +4,7 @@

= render_options(report)

%table
%table.dossier.report
%thead
%tr
- report.results.headers.each do |header|
Expand Down
8 changes: 0 additions & 8 deletions script/rails

This file was deleted.

8 changes: 4 additions & 4 deletions spec/dossier/adapter/active_record_spec.rb
Expand Up @@ -16,7 +16,7 @@
end

it "returns the connection's escaped value" do
ar_connection.stub(:quote).and_return(clean_value)
allow(ar_connection).to receive(:quote).and_return(clean_value)
expect(adapter.escape(dirty_value)).to eq(clean_value)
end

Expand All @@ -34,18 +34,18 @@
end

it "builds an adapter result" do
ar_connection.stub(:exec_query).and_return(connection_results)
allow(ar_connection).to receive(:exec_query).and_return(connection_results)
expect(adapter_result_class).to receive(:new).with(connection_results)
adapter.execute(:query)
end

it "returns the adapter result" do
ar_connection.stub(:exec_query).and_return(connection_results)
allow(ar_connection).to receive(:exec_query).and_return(connection_results)
expect(adapter.execute(:query)).to be_a(adapter_result_class)
end

it "rescues any errors and raises a Dossier::ExecuteError" do
ar_connection.stub(:exec_query).and_raise(StandardError.new('wat'))
allow(ar_connection).to receive(:exec_query).and_raise(StandardError.new('wat'))
expect{ adapter.execute(:query) }.to raise_error(Dossier::ExecuteError)
end

Expand Down

0 comments on commit abafa2f

Please sign in to comment.