vizzuality-sequel-rails 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. data/.document +5 -0
  2. data/.gitignore +29 -0
  3. data/Gemfile +3 -0
  4. data/Gemfile.lock +112 -0
  5. data/History.md +77 -0
  6. data/LICENSE +20 -0
  7. data/README.rdoc +102 -0
  8. data/Rakefile +9 -0
  9. data/autotest/discover.rb +1 -0
  10. data/lib/generators/sequel.rb +83 -0
  11. data/lib/generators/sequel/migration/migration_generator.rb +45 -0
  12. data/lib/generators/sequel/migration/templates/migration.rb +48 -0
  13. data/lib/generators/sequel/model/model_generator.rb +29 -0
  14. data/lib/generators/sequel/model/templates/migration.rb +16 -0
  15. data/lib/generators/sequel/model/templates/model.rb +6 -0
  16. data/lib/generators/sequel/observer/observer_generator.rb +19 -0
  17. data/lib/generators/sequel/observer/templates/observer.rb +7 -0
  18. data/lib/sequel-rails.rb +7 -0
  19. data/lib/sequel-rails/configuration.rb +63 -0
  20. data/lib/sequel-rails/migrations.rb +30 -0
  21. data/lib/sequel-rails/railtie.rb +93 -0
  22. data/lib/sequel-rails/railties/benchmarking_mixin.rb +23 -0
  23. data/lib/sequel-rails/railties/controller_runtime.rb +43 -0
  24. data/lib/sequel-rails/railties/database.rake +167 -0
  25. data/lib/sequel-rails/railties/i18n_support.rb +12 -0
  26. data/lib/sequel-rails/railties/log_subscriber.rb +31 -0
  27. data/lib/sequel-rails/runtime.rb +14 -0
  28. data/lib/sequel-rails/session_store.rb +82 -0
  29. data/lib/sequel-rails/setup.rb +27 -0
  30. data/lib/sequel-rails/storage.rb +222 -0
  31. data/lib/sequel-rails/version.rb +5 -0
  32. data/lib/sequel/plugins/rails_extensions.rb +35 -0
  33. data/spec/rcov.opts +6 -0
  34. data/spec/spec.opts +4 -0
  35. data/spec/spec_helper.rb +7 -0
  36. data/tasks/ci.rake +1 -0
  37. data/tasks/clean.rake +6 -0
  38. data/tasks/metrics.rake +37 -0
  39. data/tasks/spec.rake +30 -0
  40. data/tasks/yard.rake +9 -0
  41. data/tasks/yardstick.rake +20 -0
  42. data/vizzuality-sequel-rails.gemspec +29 -0
  43. metadata +208 -0
@@ -0,0 +1,45 @@
1
+ require 'generators/sequel'
2
+
3
+ module Sequel
4
+ module Generators
5
+
6
+ class MigrationGenerator < Base
7
+
8
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
9
+ class_option :id, :type => :numeric, :desc => "The id to be used in this migration"
10
+
11
+ def create_migration_file
12
+ set_local_assigns!
13
+ migration_template "migration.rb", "db/migrate/#{file_name}.rb"
14
+ end
15
+
16
+ attr_reader :migration_action, :table_action, :column_action, :use_change
17
+
18
+ protected
19
+ def set_local_assigns!
20
+ if file_name =~ /^(create|drop)_(.*)$/
21
+ @table_action = $1
22
+ @table_name = $2.pluralize
23
+ @column_action = 'add'
24
+ @use_change = @table_action == 'create' ? true : false
25
+ elsif file_name =~ /^(add|drop|remove)_.*_(?:to|from)_(.*)/
26
+ @table_action = 'alter'
27
+ @table_name = $2.pluralize
28
+ @column_action = $1 == 'add' ? 'add' : 'drop'
29
+ @use_change = @column_action == 'add' ? true : false
30
+ else
31
+ @table_action = 'alter'
32
+ if file_name =~ /^(alter)_(.*)/
33
+ @table_name = $2.pluralize
34
+ else
35
+ @table_name = file_name.pluralize
36
+ end
37
+ @use_change = false
38
+ @column_action = 'add'
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,48 @@
1
+ Sequel.migration do
2
+ <%- if use_change -%>
3
+ change do
4
+ <%= table_action %>_table :<%= table_name %> do
5
+ <%- if table_action == 'create' -%>
6
+ primary_key :id
7
+ <%- end -%>
8
+ <%- attributes.each do |attribute| -%>
9
+ <%- if table_action == 'create' -%>
10
+ <%= attribute.type_class %> :<%= attribute.name %>
11
+ <%- else -%>
12
+ <%= column_action %>_column :<%= attribute.name %><% if column_action == 'add' %>, <%= attribute.type_class %><% end %>
13
+ <%- end -%>
14
+ <%- end -%>
15
+ end
16
+ end
17
+ <%- else -%>
18
+ up do
19
+ <%- if table_action == 'drop' -%>
20
+ drop_table :<%= table_name %>
21
+ <%- else -%>
22
+ <%= table_action %>_table :<%= table_name %> do
23
+ <%- attributes.each do |attribute| -%>
24
+ <%- if table_action == 'create' -%>
25
+ <%= attribute.type_class %> :<%= attribute.name %>
26
+ <%- else -%>
27
+ <%= column_action %>_column :<%= attribute.name %><% if column_action == 'add' %>, <%= attribute.type_class %><% end %>
28
+ <%- end -%>
29
+ <%- end -%>
30
+ end
31
+ <%- end -%>
32
+ end
33
+
34
+ down do
35
+ <%- alter_table_action = (table_action == 'drop') ? 'create' : table_action -%>
36
+ <%- alter_column_action = (column_action == 'add') ? 'drop' : 'add' -%>
37
+ <%= alter_table_action %>_table :<%= table_name %> do
38
+ <%- attributes.each do |attribute| -%>
39
+ <%- if alter_table_action == 'create' -%>
40
+ <%= attribute.type_class %> :<%= attribute.name %>
41
+ <%- else -%>
42
+ <%= alter_column_action %>_column :<%= attribute.name %><% if alter_column_action == 'add' %>, <%= attribute.type_class %><% end %>
43
+ <%- end -%>
44
+ <%- end -%>
45
+ end
46
+ end
47
+ <%- end -%>
48
+ end
@@ -0,0 +1,29 @@
1
+ require 'generators/sequel'
2
+
3
+ module Sequel
4
+ module Generators
5
+
6
+ class ModelGenerator < Base
7
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
8
+
9
+ check_class_collision
10
+
11
+ class_option :migration, :type => :boolean
12
+ class_option :timestamps, :type => :boolean
13
+ class_option :parent, :type => :string, :desc => "The parent class for the generated model"
14
+
15
+ def create_migration_file
16
+ return unless options[:migration]
17
+ migration_template "migration.rb", "db/migrate/create_#{table_name}.rb"
18
+ end
19
+
20
+ def create_model_file
21
+ template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
22
+ end
23
+
24
+ hook_for :test_framework
25
+
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,16 @@
1
+ Sequel.migration do
2
+ change do
3
+
4
+ create_table :<%= table_name %> do
5
+ primary_key :id
6
+ <%- if options[:timestamps] -%>
7
+ DateTime :created_at
8
+ DateTime :updated_at
9
+ <%- end -%>
10
+ <%- attributes.each do |attribute| -%>
11
+ <%= attribute.type_class %> :<%= attribute.name %>
12
+ <%- end -%>
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,6 @@
1
+ class <%= class_name %><%= options[:parent] ? " < #{options[:parent].classify}" : " < Sequel::Model" %>
2
+ <%- if options[:timestamps] -%>
3
+ plugin :timestamps
4
+ <%- end -%>
5
+
6
+ end
@@ -0,0 +1,19 @@
1
+ require 'generators/sequel'
2
+
3
+ module Sequel
4
+ module Generators
5
+
6
+ class ObserverGenerator < Base
7
+
8
+ check_class_collision :suffix => "Observer"
9
+
10
+ def create_observer_file
11
+ template 'observer.rb', File.join('app/models', class_path, "#{file_name}_observer.rb")
12
+ end
13
+
14
+ hook_for :test_framework
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ class <%= class_name %>Observer
2
+
3
+ include Sequel::Observer
4
+
5
+ observe <%= class_name %>
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'sequel-rails/railtie'
2
+
3
+ module Rails
4
+ module Sequel
5
+ end
6
+ end
7
+
@@ -0,0 +1,63 @@
1
+ require 'active_support/core_ext/hash/except'
2
+ require 'active_support/core_ext/class/attribute_accessors'
3
+
4
+ module Rails
5
+ module Sequel
6
+
7
+ mattr_accessor :configuration
8
+
9
+ class Configuration
10
+
11
+ def self.for(root, database_yml_hash)
12
+ Rails::Sequel.configuration ||= new(root, database_yml_hash)
13
+ end
14
+
15
+ attr_reader :root, :raw
16
+ attr_accessor :logger
17
+ attr_accessor :migration_dir
18
+
19
+ def environment_for(name)
20
+ environments[name.to_s] || environments[name.to_sym]
21
+ end
22
+
23
+ def environments
24
+ @environments ||= @raw.inject({}) do |normalized, environment|
25
+ name, config = environment.first, environment.last
26
+ normalized[name] = normalize_repository_config(config)
27
+ normalized
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def initialize(root, database_yml_hash)
34
+ @root, @raw = root, database_yml_hash
35
+ end
36
+
37
+ def normalize_repository_config(hash)
38
+ config = {}
39
+ hash.each do |key, value|
40
+ config[key.to_s] =
41
+ if key.to_s == 'port'
42
+ value.to_i
43
+ elsif key.to_s == 'adapter' && value == 'sqlite3'
44
+ 'sqlite'
45
+ elsif key.to_s == 'database' && (hash['adapter'] == 'sqlite3' ||
46
+ hash['adapter'] == 'sqlite' ||
47
+ hash[:adapter] == 'sqlite3' ||
48
+ hash[:adapter] == 'sqlite')
49
+ value == ':memory:' ? value : File.expand_path((hash['database'] || hash[:database]), root)
50
+ elsif key.to_s == 'adapter' && value == 'postgresql'
51
+ 'postgres'
52
+ else
53
+ value
54
+ end
55
+ end
56
+
57
+ config
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,30 @@
1
+ require 'sequel/extensions/migration'
2
+
3
+ module Rails
4
+ module Sequel
5
+ class Migrations
6
+
7
+ class << self
8
+
9
+ def migrate_up!(version=nil)
10
+ opts = {}
11
+ opts[:target] = version.to_i if version
12
+
13
+
14
+
15
+ ::Sequel::Migrator.run(::Sequel::Model.db, "db/migrate", opts)
16
+ end
17
+
18
+ def migrate_down!(version=nil)
19
+ opts = {}
20
+ opts[:target] = version.to_i if version
21
+
22
+ ::Sequel::Migrator.run(::Sequel::Model.db, "db/migrate", opts)
23
+ end
24
+
25
+ end
26
+
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,93 @@
1
+ require 'sequel'
2
+
3
+ require 'rails'
4
+ require 'active_model/railtie'
5
+
6
+ # Comment taken from active_record/railtie.rb
7
+ #
8
+ # For now, action_controller must always be present with
9
+ # rails, so let's make sure that it gets required before
10
+ # here. This is needed for correctly setting up the middleware.
11
+ # In the future, this might become an optional require.
12
+ require 'action_controller/railtie'
13
+
14
+ require 'sequel-rails/setup'
15
+ require 'sequel-rails/railties/log_subscriber'
16
+ require 'sequel-rails/railties/i18n_support'
17
+
18
+
19
+ module Rails
20
+ module Sequel
21
+
22
+ class Railtie < Rails::Railtie
23
+
24
+ ::Sequel::Railties::LogSubscriber.attach_to :sequel
25
+
26
+ config.app_generators.orm :sequel, :migration => true
27
+ config.rails_fancy_pants_logging = true
28
+
29
+ config.action_dispatch.rescue_responses.merge!(
30
+ 'Sequel::Plugins::RailsExtensions::ModelNotFound' => :not_found,
31
+ 'Sequel::ValidationFailed' => :unprocessable_entity,
32
+ 'Sequel::NoExistingObject' => :unprocessable_entity
33
+ )
34
+
35
+ rake_tasks do
36
+ load 'sequel-rails/railties/database.rake'
37
+ end
38
+
39
+ initializer 'sequel.configuration' do |app|
40
+ configure_sequel(app)
41
+ end
42
+
43
+ initializer 'sequel.logger' do |app|
44
+ setup_logger(app, Rails.logger)
45
+ end
46
+
47
+ initializer 'sequel.i18n_support' do |app|
48
+ setup_i18n_support(app)
49
+ end
50
+
51
+ # Expose database runtime to controller for logging.
52
+ initializer 'sequel.log_runtime' do |app|
53
+ setup_controller_runtime(app)
54
+ end
55
+
56
+ initializer 'sequel.connect' do |app|
57
+ Rails::Sequel.setup(Rails.env)
58
+ end
59
+
60
+ # Run setup code after_initialize to make sure all config/initializers
61
+ # are in effect once we setup the connection. This is especially necessary
62
+ # for the cascaded adapter wrappers that need to be declared before setup.
63
+ config.after_initialize do |app|
64
+ ::Sequel::Model.plugin :active_model
65
+ ::Sequel::Model.plugin :validation_helpers
66
+ ::Sequel::Model.plugin :rails_extensions
67
+ ::Sequel::Model.raise_on_save_failure = false
68
+ end
69
+
70
+ # Support overwriting crucial steps in subclasses
71
+ def configure_sequel(app)
72
+ app.config.sequel = Rails::Sequel::Configuration.for(
73
+ Rails.root, app.config.database_configuration
74
+ )
75
+ end
76
+
77
+ def setup_i18n_support(app)
78
+ ::Sequel::Model.send :include, Rails::Sequel::I18nSupport
79
+ end
80
+
81
+ def setup_controller_runtime(app)
82
+ require 'sequel-rails/railties/controller_runtime'
83
+ ActionController::Base.send :include, Rails::Sequel::Railties::ControllerRuntime
84
+ end
85
+
86
+ def setup_logger(app, logger)
87
+ app.config.sequel.logger=logger
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,23 @@
1
+ module Sequel
2
+ module Adapters
3
+ module Benchmarking
4
+
5
+ %w[ create read update delete ].each do |method|
6
+ class_eval <<-RUBY, __FILE__, __LINE__
7
+ def #{method}(*args, &block)
8
+ result = nil
9
+ @runtime ||= 0
10
+ @runtime += Benchmark.ms { result = super(*args, &block) }
11
+ result
12
+ end
13
+ RUBY
14
+ end
15
+
16
+ def reset_runtime
17
+ rt, @runtime = @runtime, 0
18
+ rt.to_f
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,43 @@
1
+ require 'active_support/core_ext/module/attr_internal'
2
+
3
+ module Rails
4
+ module Sequel
5
+ module Railties
6
+
7
+ module ControllerRuntime
8
+
9
+ extend ActiveSupport::Concern
10
+
11
+ protected
12
+
13
+ attr_internal :db_runtime
14
+
15
+ def cleanup_view_runtime
16
+ db_rt_before_render = ::Rails::Sequel.reset_runtime
17
+ runtime = super
18
+ db_rt_after_render = ::Rails::Sequel.reset_runtime
19
+ self.db_runtime = db_rt_before_render + db_rt_after_render
20
+ runtime - db_rt_after_render
21
+ end
22
+
23
+ def append_info_to_payload(payload)
24
+ super
25
+ payload[:db_runtime] = db_runtime
26
+ end
27
+
28
+
29
+ module ClassMethods
30
+
31
+ def log_process_action(payload)
32
+ messages, db_runtime = super, payload[:db_runtime]
33
+ messages << ("Models: %.1fms" % db_runtime.to_f) if db_runtime
34
+ messages
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+ end
43
+ end