views 0.0.1 → 4.0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +1 -19
  3. data/lib/generators/view/templates/migration.rb +5 -0
  4. data/lib/generators/{views → view}/templates/view.sql +0 -0
  5. data/lib/generators/view/view_generator.rb +24 -0
  6. data/lib/views.rb +7 -50
  7. data/lib/views/extensions/active_record/abstract_adapter.rb +17 -0
  8. data/lib/views/extensions/active_record/command_recorder.rb +26 -0
  9. data/lib/views/extensions/active_record/postgresql_adapter.rb +39 -0
  10. data/lib/views/extensions/active_record/schema_dumper.rb +28 -0
  11. data/lib/views/railtie.rb +17 -0
  12. data/lib/views/version.rb +3 -1
  13. data/test/dummy/bin/bundle +1 -0
  14. data/test/dummy/bin/rails +1 -0
  15. data/test/dummy/bin/rake +1 -0
  16. data/test/dummy/bin/setup +2 -1
  17. data/test/dummy/config/database.yml +2 -5
  18. data/test/dummy/config/database.yml.travis +2 -11
  19. data/test/dummy/config/environments/development.rb +1 -1
  20. data/test/dummy/config/environments/production.rb +1 -1
  21. data/test/dummy/config/environments/test.rb +2 -2
  22. data/test/dummy/config/initializers/mime_types.rb +1 -1
  23. data/test/dummy/config/secrets.yml +2 -2
  24. data/test/dummy/db/migrate/20161125151623_create_guitars_view.rb +5 -0
  25. data/test/dummy/db/schema.rb +3 -1
  26. data/test/dummy/db/views/guitars.sql +2 -2
  27. data/test/dummy/log/development.log +2897 -24
  28. data/test/dummy/log/test.log +1545 -915
  29. data/test/dummy/public/404.html +57 -63
  30. data/test/dummy/public/422.html +57 -63
  31. data/test/dummy/public/500.html +56 -62
  32. data/test/{generators_test.rb → generator_test.rb} +4 -4
  33. data/test/migration_test.rb +20 -0
  34. data/test/task_test.rb +25 -0
  35. metadata +19 -10
  36. data/lib/generators/views/view_generator.rb +0 -15
  37. data/lib/tasks/views.rake +0 -7
  38. data/test/tasks_test.rb +0 -28
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53a95d49638eaed5ddfea7a4185eca140b6c5538
4
- data.tar.gz: 90eea8b3e9ed160311d615fae8b609aa6e9fdb3a
3
+ metadata.gz: 25680ec45b3bc9a5d63b940f368107cc9bb2a684
4
+ data.tar.gz: 0b7a296991e87bb8a597106c891d93d292d75034
5
5
  SHA512:
6
- metadata.gz: d9657814b787fbefc9bec5c04b0eaff32f751190d17d6b44e8f7166a3752a6cba4e7be79e96d32ac15dbd82f1e189e53ca7adff8a454da9bcc5e178ffe2499e6
7
- data.tar.gz: 086f75471022d40fe2ba6aacc3b1d7f57b4e00335e71f3c024e3fedac92b067ab43e0b539829b7c2de7b9367edd63362191395ec2817630104f7a6fc848dac15
6
+ metadata.gz: e0c1de49e3ce5211d3e7deaf8109aa5c1c3a7dc9805e2a34c5fd1f06e9d4cb19cf0c227878cc0815593791a97a152872eb134abc7597e2098ffcdd39852c7acc
7
+ data.tar.gz: 0272751e441afd367f1561a07a07e433e88609b3ea73cd66be2eedb5063b79751ec74a85692a2f4b8cc69c71d513d9d327d2b5ce5dc6246b56b48a7817af587d
data/Rakefile CHANGED
@@ -4,26 +4,8 @@ rescue LoadError
4
4
  puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
5
  end
6
6
 
7
- require 'rdoc/task'
8
-
9
- RDoc::Task.new(:rdoc) do |rdoc|
10
- rdoc.rdoc_dir = 'rdoc'
11
- rdoc.title = 'Views'
12
- rdoc.options << '--line-numbers'
13
- rdoc.rdoc_files.include('README.rdoc')
14
- rdoc.rdoc_files.include('lib/**/*.rb')
15
- end
16
-
17
-
18
-
19
-
20
-
21
-
22
7
  Bundler::GemHelper.install_tasks
23
8
 
24
- APP_RAKEFILE = File.expand_path('../test/dummy/Rakefile', __FILE__)
25
- load 'rails/tasks/engine.rake'
26
-
27
9
  require 'rake/testtask'
28
10
 
29
11
  Rake::TestTask.new(:test) do |t|
@@ -31,7 +13,7 @@ Rake::TestTask.new(:test) do |t|
31
13
  t.libs << 'test'
32
14
  t.pattern = 'test/**/*_test.rb'
33
15
  t.verbose = false
16
+ t.warning = false
34
17
  end
35
18
 
36
-
37
19
  task default: :test
@@ -0,0 +1,5 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def change
3
+ create_view :<%= table_name %>
4
+ end
5
+ end
File without changes
@@ -0,0 +1,24 @@
1
+ require 'rails/generators'
2
+
3
+ module Views
4
+ module Generators
5
+ class ViewGenerator < ::Rails::Generators::NamedBase
6
+ include Rails::Generators::Migration
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ def create_view_file
11
+ template 'view.sql', "db/views/#{table_name}.sql"
12
+ end
13
+
14
+ def create_migration_file
15
+ migration_template 'migration.rb', "db/migrate/create_#{table_name}_view.rb"
16
+ end
17
+
18
+ def self.next_migration_number(path)
19
+ Time.now.utc.strftime '%Y%m%d%H%M%S'
20
+ end
21
+
22
+ end
23
+ end
24
+ end
data/lib/views.rb CHANGED
@@ -1,52 +1,9 @@
1
- module Views
2
- class << self
3
-
4
- def sync
5
- pattern = Rails.root.join('db/views/*.sql')
6
- Dir.glob(pattern) do |path|
7
- path = Pathname.new(path)
8
- if changed?(path)
9
- name = path.basename('.sql')
10
- sql = File.read(path)
11
- puts "Updating #{name}"
12
- connection.execute "DROP VIEW IF EXISTS #{name}"
13
- connection.execute "CREATE VIEW #{name} AS \n #{sql}"
14
- end
15
- end
16
- end
17
-
18
- private
19
-
20
- def connection
21
- ActiveRecord::Base.connection
22
- end
1
+ require 'views/extensions/active_record/abstract_adapter'
2
+ require 'views/extensions/active_record/command_recorder'
3
+ require 'views/extensions/active_record/postgresql_adapter'
4
+ require 'views/extensions/active_record/schema_dumper'
5
+ require 'views/railtie'
6
+ require 'views/version'
23
7
 
24
- def changed?(path)
25
- basename = path.basename.to_s
26
- digest_path = digests_path.join(basename)
27
- current_digest = Digest::MD5.file(path).hexdigest
28
- if File.exist?(digest_path)
29
- last_digest = File.read(digest_path)
30
- if last_digest != current_digest
31
- update_digest digest_path, current_digest
32
- true
33
- else
34
- false
35
- end
36
- else
37
- update_digest digest_path, current_digest
38
- true
39
- end
40
- end
41
-
42
- def update_digest(path, value)
43
- FileUtils.mkdir_p path.dirname
44
- File.write path, value
45
- end
46
-
47
- def digests_path
48
- Rails.root.join 'tmp/digests/db/views'
49
- end
50
-
51
- end
8
+ module Views
52
9
  end
@@ -0,0 +1,17 @@
1
+ module Views
2
+ module Extensions
3
+ module ActiveRecord
4
+ module AbstractAdapter
5
+ extend ActiveSupport::Concern
6
+
7
+ def initialize(*args)
8
+ if adapter_name == 'PostgreSQL'
9
+ self.class.include Views::Extensions::ActiveRecord::PostgreSQLAdapter
10
+ end
11
+ super
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ module Views
2
+ module Extensions
3
+ module ActiveRecord
4
+ module CommandRecorder
5
+ extend ActiveSupport::Concern
6
+
7
+ def create_view(args)
8
+ record :create_view, args
9
+ end
10
+
11
+ def change_view(args)
12
+ record :change_view, args
13
+ end
14
+
15
+ def drop_view(args)
16
+ record :drop_view, args
17
+ end
18
+
19
+ def invert_create_view(args)
20
+ [:drop_view, args]
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,39 @@
1
+ module Views
2
+ module Extensions
3
+ module ActiveRecord
4
+ module PostgreSQLAdapter
5
+ extend ActiveSupport::Concern
6
+
7
+ def create_view(name, options={})
8
+ if options[:force]
9
+ execute "DROP VIEW IF EXISTS #{name}"
10
+ end
11
+ path = Rails.root.join("db/views/#{name}.sql")
12
+ definition = File.read(path)
13
+ execute "CREATE VIEW #{name} AS #{definition}"
14
+ end
15
+
16
+ def change_view(name, options={})
17
+ drop_view name
18
+ create_view name, options
19
+ end
20
+
21
+ def drop_view(name)
22
+ execute "DROP VIEW #{name}"
23
+ end
24
+
25
+ def views
26
+ execute <<-SQL
27
+ SELECT c.relname as name, c.relkind AS type
28
+ FROM pg_class c
29
+ LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
30
+ WHERE c.relkind IN ('v')
31
+ AND n.nspname = ANY (current_schemas(false))
32
+ ORDER BY c.oid ASC
33
+ SQL
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,28 @@
1
+ module Views
2
+ module Extensions
3
+ module ActiveRecord
4
+ module SchemaDumper
5
+ extend ActiveSupport::Concern
6
+
7
+ def dump(stream)
8
+ header(stream)
9
+ extensions(stream)
10
+ tables(stream)
11
+ views(stream)
12
+ trailer(stream)
13
+ stream
14
+ end
15
+
16
+ def views(stream)
17
+ @connection.views.map(&:values).each do |name, type|
18
+ stream.puts <<-SCHEMA.strip_heredoc.indent(2)
19
+ create_view "#{name}", force: true
20
+ SCHEMA
21
+ end
22
+ stream.puts "\n"
23
+ end
24
+
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ module Views
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer 'views.extensions' do
5
+ ::ActiveRecord::Migration::CommandRecorder.include(
6
+ Views::Extensions::ActiveRecord::CommandRecorder
7
+ )
8
+ ::ActiveRecord::ConnectionAdapters::AbstractAdapter.include(
9
+ Views::Extensions::ActiveRecord::AbstractAdapter
10
+ )
11
+ ::ActiveRecord::SchemaDumper.prepend(
12
+ Views::Extensions::ActiveRecord::SchemaDumper
13
+ )
14
+ end
15
+
16
+ end
17
+ end
data/lib/views/version.rb CHANGED
@@ -1,3 +1,5 @@
1
1
  module Views
2
- VERSION = '0.0.1'
2
+
3
+ VERSION = '4.0.0.0'
4
+
3
5
  end
@@ -1,3 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
+
2
3
  ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
4
  load Gem.bin_path('bundler', 'bundle')
data/test/dummy/bin/rails CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+
2
3
  APP_PATH = File.expand_path('../../config/application', __FILE__)
3
4
  require_relative '../config/boot'
4
5
  require 'rails/commands'
data/test/dummy/bin/rake CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+
2
3
  require_relative '../config/boot'
3
4
  require 'rake'
4
5
  Rake.application.run
data/test/dummy/bin/setup CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+
2
3
  require 'pathname'
3
4
 
4
5
  # path to your application root.
@@ -8,7 +9,7 @@ Dir.chdir APP_ROOT do
8
9
  # This script is a starting point to setup your application.
9
10
  # Add necessary setup steps to this file:
10
11
 
11
- puts "== Installing dependencies =="
12
+ puts '== Installing dependencies =='
12
13
  system 'gem install bundler --conservative'
13
14
  system 'bundle check || bundle install'
14
15
 
@@ -1,10 +1,7 @@
1
- default: &default
2
- adapter: postgresql
3
-
4
1
  development:
5
- <<: *default
2
+ adapter: postgresql
6
3
  database: views_development
7
4
 
8
5
  test:
9
- <<: *default
6
+ adapter: postgresql
10
7
  database: views_test
@@ -1,12 +1,3 @@
1
- mysql: &mysql
2
- adapter: <%= 'jdbc' if RUBY_ENGINE == 'jruby' %>mysql<%= '2' if RUBY_ENGINE != 'jruby' %>
3
-
4
- postgres: &postgres
5
- adapter: <%= 'jdbc' if RUBY_ENGINE == 'jruby' %>postgresql
6
-
7
- sqlite: &sqlite
8
- adapter: <%= 'jdbc' if RUBY_ENGINE == 'jruby' %>sqlite3
9
-
10
1
  test:
11
- <<: *<%= ENV['DB'] %>
12
- database: <%= ENV['DB'] == 'sqlite' ? 'db/travis.sqlite3' : 'travis' %>
2
+ adapter: <%= 'jdbc' if RUBY_ENGINE == 'jruby' %>postgresql
3
+ database: travis
@@ -10,7 +10,7 @@ Rails.application.configure do
10
10
  config.eager_load = false
11
11
 
12
12
  # Show full error reports and disable caching.
13
- config.consider_all_requests_local = true
13
+ config.consider_all_requests_local = true
14
14
  config.action_controller.perform_caching = false
15
15
 
16
16
  # Don't care if the mailer can't send.
@@ -11,7 +11,7 @@ Rails.application.configure do
11
11
  config.eager_load = true
12
12
 
13
13
  # Full error reports are disabled and caching is turned on.
14
- config.consider_all_requests_local = false
14
+ config.consider_all_requests_local = false
15
15
  config.action_controller.perform_caching = true
16
16
 
17
17
  # Enable Rack::Cache to put a simple HTTP cache in front of your application
@@ -13,11 +13,11 @@ Rails.application.configure do
13
13
  config.eager_load = false
14
14
 
15
15
  # Configure static file server for tests with Cache-Control for performance.
16
- config.serve_static_files = true
16
+ config.serve_static_files = true
17
17
  config.static_cache_control = 'public, max-age=3600'
18
18
 
19
19
  # Show full error reports and disable caching.
20
- config.consider_all_requests_local = true
20
+ config.consider_all_requests_local = true
21
21
  config.action_controller.perform_caching = false
22
22
 
23
23
  # Raise exceptions instead of rendering exception templates.
@@ -1,4 +1,4 @@
1
1
  # Be sure to restart your server when you modify this file.
2
2
 
3
3
  # Add new mime types for use in respond_to blocks:
4
- # Mime::Type.register "text/richtext", :rtf
4
+ # Mime::Type.register 'text/richtext', :rtf
@@ -11,10 +11,10 @@
11
11
  # if you're sharing your code publicly.
12
12
 
13
13
  development:
14
- secret_key_base: a462a71b37fda26c6c5bb2512ba1c4409a7e98106ec5544e659a214a830e818b30323861f5edac24b84eb94a3da0aceb5fe71324eb4162af1bdfb932791e82c1
14
+ secret_key_base: 2c1c8d4cbaa726b21aa6483b7d556125f4897508e2b94f8b3ddaec675168382c9b3b6eb5a9359d2fade03f539c16ac1ef905891c2410f2fd00b83b76c1666feb
15
15
 
16
16
  test:
17
- secret_key_base: bca8c7ab4adb2e383b8febce8f8890fe46d44424e69636ae5abe55b05a79a2109272a262560ad63df5f2575c459cc37107cbc5e6ad134be0191d58c0be70db7e
17
+ secret_key_base: 9dd531171128e7c3d11dd2c5c18c84ba43d29b677043002634a6f4d58bf2687a283b7b6dc6af741d63c3824f11fa1f858010d7c2509a932023f2ece0d3bfe6cf
18
18
 
19
19
  # Do not keep production secrets in the repository,
20
20
  # instead read values from the environment.
@@ -0,0 +1,5 @@
1
+ class CreateGuitarsView < ActiveRecord::Migration
2
+ def change
3
+ create_view :guitars
4
+ end
5
+ end
@@ -11,7 +11,7 @@
11
11
  #
12
12
  # It's strongly recommended that you check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(version: 20161017172847) do
14
+ ActiveRecord::Schema.define(version: 20161125151623) do
15
15
 
16
16
  # These are extensions that must be enabled in order to support this database
17
17
  enable_extension "plpgsql"
@@ -23,4 +23,6 @@ ActiveRecord::Schema.define(version: 20161017172847) do
23
23
  t.datetime "updated_at", null: false
24
24
  end
25
25
 
26
+ create_view "guitars", force: true
27
+
26
28
  end
@@ -1,7 +1,7 @@
1
1
 
2
2
  SELECT
3
- products.*
3
+ *
4
4
  FROM
5
5
  products
6
6
  WHERE
7
- products.category = 'Guitar'
7
+ category = 'Guitar'