listable 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.2.3"
12
+ gem "jeweler", "~> 1.8.4"
13
+ gem "simplecov", ">= 0"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,44 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.2.11)
5
+ i18n (~> 0.6)
6
+ multi_json (~> 1.0)
7
+ bourne (1.1.2)
8
+ mocha (= 0.10.5)
9
+ git (1.2.5)
10
+ i18n (0.6.1)
11
+ jeweler (1.8.4)
12
+ bundler (~> 1.0)
13
+ git (>= 1.2.5)
14
+ rake
15
+ rdoc
16
+ json (1.7.6)
17
+ metaclass (0.0.1)
18
+ mocha (0.10.5)
19
+ metaclass (~> 0.0.1)
20
+ multi_json (1.5.0)
21
+ rake (10.0.3)
22
+ rdoc (3.12)
23
+ json (~> 1.4)
24
+ shoulda (3.3.2)
25
+ shoulda-context (~> 1.0.1)
26
+ shoulda-matchers (~> 1.4.1)
27
+ shoulda-context (1.0.2)
28
+ shoulda-matchers (1.4.2)
29
+ activesupport (>= 3.0.0)
30
+ bourne (~> 1.1.2)
31
+ simplecov (0.7.1)
32
+ multi_json (~> 1.0)
33
+ simplecov-html (~> 0.7.1)
34
+ simplecov-html (0.7.1)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ bundler (~> 1.2.3)
41
+ jeweler (~> 1.8.4)
42
+ rdoc (~> 3.12)
43
+ shoulda
44
+ simplecov
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Johannes Baldursson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,24 @@
1
+ = listable
2
+
3
+ With listable you can consolidate fields from several models into one, backed up by a database view.
4
+ It is perfect for e.g. a front page where you may want to display the most recent additions to your site in a joint list.
5
+ By providing scopes for each model you wish to include in a view, Listable will automatically create the database view for you.
6
+
7
+ == How to use
8
+ Coming soon...
9
+
10
+ == Contributing to listable
11
+
12
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
13
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
14
+ * Fork the project.
15
+ * Start a feature/bugfix branch.
16
+ * Commit and push until you are happy with your contribution.
17
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
18
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
19
+
20
+ == Copyright
21
+
22
+ Copyright (c) 2013 Johannes Baldursson. See LICENSE.txt for
23
+ further details.
24
+
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "listable"
18
+ gem.homepage = "http://github.com/baldursson/listable"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Rails extension that makes it easy to list and query several models through a single view backed model.}
21
+ gem.description = %Q{With listable you can consolidate fields from several models into one, backed up by a database view.
22
+ It is perfect for e.g. a front page where you may want to display the most recent additions to your site in a joint list.
23
+ By providing scopes for each model you wish to include in a view, Listable will automatically create the database view for you.}
24
+ gem.email = "johannes.baldursson@gmail.com"
25
+ gem.authors = ["Johannes Baldursson"]
26
+ # dependencies defined in Gemfile
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rake/testtask'
31
+ Rake::TestTask.new(:test) do |test|
32
+ test.libs << 'lib' << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+
37
+ # require 'simplecov/rcovtask'
38
+ # Rcov::RcovTask.new do |test|
39
+ # test.libs << 'test'
40
+ # test.pattern = 'test/**/test_*.rb'
41
+ # test.verbose = true
42
+ # test.rcov_opts << '--exclude "gems/*"'
43
+ # end
44
+
45
+ task :default => :test
46
+
47
+ require 'rdoc/task'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "listable #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/lib/listable.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'active_support/concern'
2
+
3
+ require 'listable/view_manager'
4
+ require 'listable/connection_adapters'
5
+ require 'listable/querying'
6
+ require 'listable/railtie' if defined?(Rails)
7
+
8
+ module Listable
9
+ extend ActiveSupport::Concern
10
+
11
+ module ClassMethods
12
+ def listable_through(listable_view_name, scope_name)
13
+ has_one listable_view_name.to_s.singularize, as: :listable
14
+
15
+ ViewManager.add_listable listable_view_name, self.name, scope_name
16
+ end
17
+
18
+ def acts_as_listable_view
19
+ self.table_name = ViewManager.prefixed_view_name(self.name)
20
+ belongs_to :listable, polymorphic: true
21
+ include ViewMethods
22
+ end
23
+ end
24
+
25
+ module ViewMethods
26
+ def hash
27
+ listable_id.hash + listable_type.hash
28
+ end
29
+
30
+ def eql?(result)
31
+ listable_type == result.listable_type and
32
+ listable_id == result.listable_id
33
+ end
34
+
35
+ def readonly?
36
+ true
37
+ end
38
+ end
39
+ end
40
+
41
+ class ActiveRecord::Base
42
+ include Listable
43
+ include Listable::Querying
44
+ end
45
+
46
+ class ActiveRecord::ConnectionAdapters::AbstractAdapter
47
+ include Listable::ConnectionAdapters::SchemaStatements
48
+ end
49
+
50
+
@@ -0,0 +1,78 @@
1
+ module Listable
2
+ module ConnectionAdapters
3
+
4
+ module SchemaStatements
5
+ def create_view(view_name, queries)
6
+ create_sql = "CREATE VIEW #{view_name.to_s.pluralize} AS "
7
+ queries.map!(&:to_sql) # Compile the arel queries to sql
8
+ create_sql << queries * ' UNION ' # Combines the queries with union
9
+
10
+ execute create_sql
11
+ end
12
+
13
+ def drop_view(view_name)
14
+ execute "DROP VIEW #{view_name}"
15
+ end
16
+ end
17
+
18
+ module ConcatenationMethods
19
+ module PipeOperator
20
+ def concat(args)
21
+ args * " || "
22
+ end
23
+ end
24
+
25
+ module PlusOperator
26
+ def concat(args)
27
+ args * " + "
28
+ end
29
+ end
30
+
31
+ module Function
32
+ def concat(args)
33
+ "CONCAT(#{args * ', '})"
34
+ end
35
+ end
36
+ end
37
+
38
+ module PostgreSQLExtensions
39
+ include ConcatenationMethods::PipeOperator
40
+
41
+ def views
42
+ query(<<-SQL, 'SCHEMA').map { |row| row[0] }
43
+ SELECT table_name
44
+ FROM INFORMATION_SCHEMA.views
45
+ WHERE table_schema = ANY (current_schemas(false))
46
+ SQL
47
+ end
48
+ end
49
+
50
+ module SQLiteExtensions
51
+ include ConcatenationMethods::PipeOperator
52
+
53
+ def views
54
+ exec_query(<<-SQL, 'SCHEMA').map { |row| row['name'] }
55
+ SELECT name
56
+ FROM sqlite_master
57
+ WHERE type = 'view';
58
+ SQL
59
+ end
60
+ end
61
+
62
+ module MySQLExtensions
63
+ include ConcatenationMethods::Function
64
+
65
+ def views()
66
+ query = <<-SQL
67
+ SHOW FULL TABLES
68
+ IN #{quote_table_name(current_database)}
69
+ WHERE TABLE_TYPE LIKE 'VIEW'
70
+ SQL
71
+
72
+ execute_and_free(query, 'SCHEMA') do |result|
73
+ result.collect { |field| field.first }
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,29 @@
1
+ module Listable
2
+ module Querying
3
+
4
+ module ClassMethods
5
+ def concat_select(fields, as_name)
6
+ fields.map! do |field|
7
+ if field.is_a? Symbol
8
+ connection.quote_column_name(field)
9
+ else
10
+ connection.quote(field)
11
+ end
12
+ end
13
+ select("#{connection.concat(fields)} AS #{as_name}")
14
+ end
15
+
16
+ def select_as(fields)
17
+ selection = []
18
+ fields.each do |field, as_name|
19
+ selection << "#{connection.quote_column_name(field)} AS #{as_name}"
20
+ end
21
+ select(selection * ', ')
22
+ end
23
+ end
24
+
25
+ def self.included(receiver)
26
+ receiver.extend ClassMethods
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,35 @@
1
+ #require 'rake'
2
+
3
+ module Listable
4
+ class Railtie < Rails::Railtie
5
+ initializer "listable" do
6
+ # Rebuild views after db:migrate
7
+ # Rake::Task['db:migrate'].enhance do
8
+ # Rake::Task['listable:migrate'].invoke
9
+ # end
10
+
11
+ ActiveSupport.on_load :active_record do
12
+ # Extending connection adapters
13
+ if ActiveRecord::ConnectionAdapters.const_defined?(:PostgreSQLAdapter)
14
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send(:include, Listable::ConnectionAdapters::PostgreSQLExtensions)
15
+ end
16
+
17
+ if ActiveRecord::ConnectionAdapters.const_defined?(:SQLiteAdapter)
18
+ ActiveRecord::ConnectionAdapters::SQLiteAdapter.send(:include, Listable::ConnectionAdapters::SQLiteExtensions)
19
+ end
20
+
21
+ if ActiveRecord::ConnectionAdapters.const_defined?(:MysqlAdapter)
22
+ ActiveRecord::ConnectionAdapters::MysqlAdapter.send(:include, Listable::ConnectionAdapters::MySQLExtensions)
23
+ end
24
+
25
+ if ActiveRecord::ConnectionAdapters.const_defined?(:Mysql2Adapter)
26
+ ActiveRecord::ConnectionAdapters::Mysql2Adapter.send(:include, Listable::ConnectionAdapters::MySQLExtensions)
27
+ end
28
+ end
29
+ end
30
+
31
+ rake_tasks do
32
+ load 'tasks/listable.rake'
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,51 @@
1
+ module Listable
2
+ class ViewManager
3
+ cattr_reader :listables
4
+
5
+ class << self
6
+ def prefixed_view_name(name)
7
+ prefix << name.underscore.pluralize
8
+ end
9
+
10
+ def prefix
11
+ 'lstble_'
12
+ end
13
+
14
+ def add_listable(view_name, model_name, model_scope_name)
15
+ @@listables ||= {}
16
+ @@listables[view_name] ||= {}
17
+ @@listables[view_name][model_name] = model_scope_name
18
+ end
19
+
20
+ def listable_view?(name)
21
+ name.start_with? prefix
22
+ end
23
+
24
+ def create_views
25
+ ActiveRecord::Base.transaction do
26
+ drop_views # First drop all listable views to get a fresh start
27
+ listables.each do |view_name, query_info|
28
+ queries = []
29
+ view_name = prefixed_view_name(view_name.to_s) # Appending a prefix to views
30
+ query_info.each do |model_name, scope|
31
+ model_class = Kernel.const_get(model_name)
32
+ query = model_class.select_as(id: :listable_id) # Always begin selection with the original model ID
33
+ query = query.send(scope) # Appends selection from scope
34
+ query = query.select([:created_at, :updated_at]) # Include Rails' timestamps in view
35
+ query = query.select("CAST('#{model_name}' AS char) AS listable_type") # Finish with the original model name, needed for the polymorphic relation
36
+
37
+ queries << query
38
+ end
39
+ ActiveRecord::Base.connection.create_view view_name, queries
40
+ end
41
+ end
42
+ end
43
+
44
+ def drop_views
45
+ ActiveRecord::Base.connection.views.each do |name|
46
+ ActiveRecord::Base.connection.drop_view(name) if listable_view? name
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,13 @@
1
+ namespace :listable do
2
+ desc "(Re)creates all listable views"
3
+ task :migrate => :environment do
4
+ puts "Creating Listable views..."
5
+ Rails.application.eager_load!
6
+ Listable::ViewManager.create_views
7
+ end
8
+ end
9
+
10
+ # Rebuild views after db:migrate
11
+ Rake::Task['db:migrate'].enhance do
12
+ Rake::Task['listable:migrate'].invoke
13
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'listable'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestListable < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: listable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Johannes Baldursson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: shoulda
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rdoc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.12'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.12'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.2.3
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.2.3
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.4
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.4
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: ! "With listable you can consolidate fields from several models into
95
+ one, backed up by a database view.\n It is perfect for e.g.
96
+ a front page where you may want to display the most recent additions to your site
97
+ in a joint list.\n By providing scopes for each model you
98
+ wish to include in a view, Listable will automatically create the database view
99
+ for you."
100
+ email: johannes.baldursson@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files:
104
+ - LICENSE.txt
105
+ - README.rdoc
106
+ files:
107
+ - .document
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.rdoc
112
+ - Rakefile
113
+ - VERSION
114
+ - lib/listable.rb
115
+ - lib/listable/connection_adapters.rb
116
+ - lib/listable/querying.rb
117
+ - lib/listable/railtie.rb
118
+ - lib/listable/view_manager.rb
119
+ - lib/tasks/listable.rake
120
+ - test/helper.rb
121
+ - test/test_listable.rb
122
+ homepage: http://github.com/baldursson/listable
123
+ licenses:
124
+ - MIT
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ segments:
136
+ - 0
137
+ hash: -3454069564760365007
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 1.8.19
147
+ signing_key:
148
+ specification_version: 3
149
+ summary: Rails extension that makes it easy to list and query several models through
150
+ a single view backed model.
151
+ test_files: []