summaryrefslogtreecommitdiff
path: root/lib/extensions
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2014-04-08 09:12:37 +0200
committerAzul <azul@leap.se>2014-04-08 09:12:37 +0200
commit53808b073f539ba2b442738b6abf97228488e311 (patch)
tree67e344defee90e4d0c5f91f6136f6619e97c4ace /lib/extensions
parentcb6442c344d6bdaf52c3878b2de2fcf4d85f2648 (diff)
moving all of core into toplevel, tests fail.
Diffstat (limited to 'lib/extensions')
-rw-r--r--lib/extensions/couchrest.rb95
-rw-r--r--lib/extensions/testing.rb48
2 files changed, 143 insertions, 0 deletions
diff --git a/lib/extensions/couchrest.rb b/lib/extensions/couchrest.rb
new file mode 100644
index 0000000..a9a195e
--- /dev/null
+++ b/lib/extensions/couchrest.rb
@@ -0,0 +1,95 @@
+module CouchRest
+ module Model
+ module Designs
+
+ class View
+
+ # so we can called Ticket.method.descending or Ticket.method.ascending
+ def ascending
+ self
+ end
+ end
+
+ class DesignMapper
+ def load_views(dir)
+ Dir.glob("#{dir}/*.js") do |js|
+ name = File.basename(js, '.js')
+ file = File.open(js, 'r')
+ view name.to_sym,
+ :map => file.read,
+ :reduce => "function(key, values, rereduce) { return sum(values); }"
+ end
+ end
+ end
+ end
+
+ module Connection
+
+ module ClassMethods
+
+ def use_database(db)
+ @database = prepare_database(db)
+ rescue RestClient::Exception,
+ Errno::EHOSTUNREACH,
+ Errno::ECONNREFUSED => e
+ message = "Could not connect to couch database #{db} due to #{e.to_s}"
+ Rails.logger.warn message
+ raise e.class.new(message) if APP_CONFIG[:reraise_errors]
+ end
+ end
+
+ end
+
+ module Utils
+ module Migrate
+ def self.load_all_models_with_engines
+ self.load_all_models_without_engines
+ return unless defined?(Rails)
+ Dir[Rails.root + '*/app/models/**/*.rb'].each do |path|
+ require path
+ end
+ end
+
+ class << self
+ alias_method_chain :load_all_models, :engines
+ end
+
+ def dump_all_models
+ prepare_directory
+ find_models.each do |model|
+ model.design_docs.each do |design|
+ dump_design(model, design)
+ end
+ end
+ end
+
+ protected
+
+ def dump_design(model, design)
+ dir = prepare_directory model.name.tableize
+ filename = design.id.sub('_design/','') + '.json'
+ puts dir + filename
+ design.checksum
+ File.open(dir + filename, "w") do |file|
+ file.write(JSON.pretty_generate(design.to_hash))
+ end
+ end
+
+ def prepare_directory(dir = '')
+ dir = Rails.root + 'tmp' + 'designs' + dir
+ Dir.mkdir(dir) unless Dir.exists?(dir)
+ return dir
+ end
+
+ end
+ end
+
+ end
+
+ class ModelRailtie
+ config.action_dispatch.rescue_responses.merge!(
+ 'CouchRest::Model::DocumentNotFound' => :not_found,
+ 'RestClient::ResourceNotFound' => :not_found
+ )
+ end
+end
diff --git a/lib/extensions/testing.rb b/lib/extensions/testing.rb
new file mode 100644
index 0000000..8f7e73c
--- /dev/null
+++ b/lib/extensions/testing.rb
@@ -0,0 +1,48 @@
+module LeapWebCore
+ module AssertResponses
+
+ # response that works with different TestCases:
+ # ActionController::TestCase has @response
+ # ActionDispatch::IntegrationTest has @response
+ # Rack::Test::Methods defines last_response
+ def get_response
+ @response || last_response
+ end
+
+ def assert_attachement_filename(name)
+ assert_equal %Q(attachment; filename="#{name}"),
+ get_response.headers["Content-Disposition"]
+ end
+
+ def json_response
+ response = JSON.parse(get_response.body)
+ response.respond_to?(:with_indifferent_access) ?
+ response.with_indifferent_access :
+ response
+ end
+
+ def assert_json_response(object)
+ assert_equal 'application/json',
+ get_response.content_type.to_s.split(';').first
+ if object.is_a? Hash
+ object.stringify_keys! if object.respond_to? :stringify_keys!
+ assert_equal object, json_response
+ else
+ assert_equal object.to_json, get_response.body
+ end
+ end
+
+ def assert_json_error(object)
+ object.stringify_keys! if object.respond_to? :stringify_keys!
+ assert_json_response :errors => object
+ end
+ end
+end
+
+class ::ActionController::TestCase
+ include LeapWebCore::AssertResponses
+end
+
+class ::ActionDispatch::IntegrationTest
+ include LeapWebCore::AssertResponses
+end