tableling-rails 0.0.17 → 0.0.18

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.
@@ -69,8 +69,8 @@ module Tableling
69
69
  end
70
70
 
71
71
  def serialize value
72
- serializer = @view.settings.serializers.find{ |s| s.match? value }
73
- serializer ? serializer.serialize(value) : value
72
+ type_serializer = @view.settings.type_serializers.find{ |s| s.match? value }
73
+ type_serializer ? type_serializer.serialize(value) : value
74
74
  end
75
75
  end
76
76
  end
@@ -5,26 +5,26 @@ module Tableling
5
5
 
6
6
  def initialize parent = nil
7
7
  @parent = parent
8
- @serializers = []
8
+ @type_serializers = []
9
9
  end
10
10
 
11
11
  def configure &block
12
12
  instance_eval &block if block
13
13
  end
14
14
 
15
- def serialize type, &block
16
- @serializers << Serializer.new(type, block)
15
+ def serialize_type type, &block
16
+ @type_serializers << TypeSerializer.new(type, block)
17
17
  end
18
18
 
19
- def serializers
20
- (@parent ? @parent.serializers : []) + @serializers.dup
19
+ def type_serializers
20
+ (@parent ? @parent.type_serializers : []) + @type_serializers
21
21
  end
22
22
 
23
23
  def dsl
24
24
  m = Module.new do
25
25
 
26
- def serialize type, &block
27
- @settings.serialize type, &block
26
+ def serialize_type type, &block
27
+ @settings.serialize_type type, &block
28
28
  end
29
29
  end
30
30
  m.instance_variable_set :@settings, self
@@ -1,7 +1,7 @@
1
1
 
2
2
  module Tableling
3
3
 
4
- class Serializer
4
+ class TypeSerializer
5
5
 
6
6
  def initialize type, block
7
7
  @type, @block = type, block
@@ -1,3 +1,3 @@
1
1
  module Tableling
2
- VERSION = "0.0.17"
2
+ VERSION = "0.0.18"
3
3
  end
@@ -2,7 +2,8 @@
2
2
  module Tableling
3
3
 
4
4
  class View
5
- attr_reader :name, :config, :settings, :base_query, :base_count_query
5
+ attr_reader :name, :config, :settings, :fields
6
+ attr_accessor :base_query, :base_count_query, :quick_search_block, :serialize_record_block, :serialize_response_block
6
7
 
7
8
  def initialize name, config, options = {}, &block
8
9
 
@@ -13,28 +14,16 @@ module Tableling
13
14
  @base_count_query = nil
14
15
 
15
16
  @settings = Settings.new @config.settings
16
- extend @settings.dsl
17
17
 
18
- instance_eval &block if block
19
- @frozen = true
20
- end
21
-
22
- def field name, options = {}, &block
23
- return @fields.find{ |f| f.name.to_s == name.to_s } if @frozen
24
- @fields.delete_if{ |f| f.name.to_s == name.to_s }
25
- Field.new(name, self, options, &block).tap{ |f| @fields << f }
26
- end
27
-
28
- def quick_search &block
29
- @quick_search = block
30
- end
31
-
32
- def base base_query
33
- @base_query = base_query
18
+ if block
19
+ dsl = DSL.new self
20
+ dsl.extend @settings.dsl
21
+ dsl.instance_eval &block
22
+ end
34
23
  end
35
24
 
36
- def base_count base_count_query
37
- @base_count_query = base_count_query
25
+ def field name
26
+ @fields.find{ |f| f.name.to_s == name.to_s }
38
27
  end
39
28
 
40
29
  def process options = {}
@@ -42,9 +31,9 @@ module Tableling
42
31
  q = options[:base] || @base_query
43
32
  cq = options[:base_count] || @base_count_query
44
33
 
45
- if @quick_search and options[:quickSearch].present?
46
- q = @quick_search.call q, options[:quickSearch].to_s
47
- cq = @quick_search.call cq, options[:quickSearch].to_s if cq
34
+ if @quick_search_block and options[:quickSearch].present?
35
+ q = @quick_search_block.call q, options[:quickSearch].to_s
36
+ cq = @quick_search_block.call cq, options[:quickSearch].to_s if cq
48
37
  end
49
38
 
50
39
  total = (cq || q).count
@@ -68,12 +57,52 @@ module Tableling
68
57
  offset = 0 if offset < 0
69
58
  q = q.offset offset * limit
70
59
 
71
- {
72
- :total => total,
73
- :data => q.all.collect{ |o|
74
- @fields.inject({}){ |memo,f| memo[f.name] = f.extract(o); memo }
75
- }
60
+ serialize_response total, q
61
+ end
62
+
63
+ class DSL
64
+
65
+ def initialize view
66
+ @view = view
67
+ end
68
+
69
+ def field name, options = {}, &block
70
+ @view.fields.delete_if{ |f| f.name.to_s == name.to_s }
71
+ Field.new(name, @view, options, &block).tap{ |f| @view.fields << f }
72
+ end
73
+
74
+ %w(base base_count).each do |m|
75
+ define_method m do |q|
76
+ @view.send "#{m}_query=", q
77
+ end
78
+ end
79
+
80
+ %w(quick_search serialize_record serialize_response).each do |m|
81
+ define_method m do |&block|
82
+ @view.send "#{m}_block=", block
83
+ end
84
+ end
85
+ end
86
+
87
+ private
88
+
89
+ def serialize_response total, query
90
+
91
+ res = {
92
+ total: total,
93
+ data: query.all
76
94
  }
95
+
96
+ if @serialize_response_block
97
+ @serialize_response_block.call res
98
+ else
99
+ res[:data] = res[:data].collect{ |r| serialize_record r }
100
+ res
101
+ end
102
+ end
103
+
104
+ def serialize_record record
105
+ @serialize_record_block ? @serialize_record_block.call(record) : @fields.inject({}){ |memo,f| memo[f.name] = f.extract(record); memo }
77
106
  end
78
107
  end
79
108
  end
@@ -1,13 +1,17 @@
1
1
  class Book < ActiveRecord::Base
2
+ include Tableling::Model
2
3
 
3
4
  tableling do
4
5
 
5
- field :title
6
- field :author
6
+ default_view do
7
7
 
8
- quick_search do |query,term|
9
- term = "%#{term.downcase}%"
10
- query.where('LOWER(books.title) LIKE ? OR LOWER(books.author) LIKE ?', term, term)
8
+ field :title
9
+ field :author
10
+
11
+ quick_search do |query,term|
12
+ term = "%#{term.downcase}%"
13
+ query.where('LOWER(books.title) LIKE ? OR LOWER(books.author) LIKE ?', term, term)
14
+ end
11
15
  end
12
16
  end
13
17
  end
Binary file
Binary file
@@ -1,59 +1,52 @@
1
1
  Connecting to database specified by database.yml
2
-  (0.2ms) select sqlite_version(*)
3
-  (3.8ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
4
-  (0.0ms) PRAGMA index_list("schema_migrations")
5
-  (1.1ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
6
-  (2.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
2
+  (0.1ms) select sqlite_version(*)
3
+  (5.7ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
4
+  (1.3ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
5
+  (2.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
7
6
  Connecting to database specified by database.yml
8
-  (2.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
7
+  (1.4ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
9
8
  Migrating to CreateBooks (20121008115302)
10
9
   (0.0ms) select sqlite_version(*)
11
10
   (0.0ms) begin transaction
12
-  (0.7ms) CREATE TABLE "books" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255) NOT NULL, "author" varchar(255) NOT NULL, "year" integer NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
13
-  (0.0ms) PRAGMA index_list("books")
14
-  (0.3ms) CREATE UNIQUE INDEX "index_books_on_title" ON "books" ("title")
15
-  (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES ('20121008115302')
16
-  (3.0ms) commit transaction
17
-  (0.3ms) select sqlite_version(*)
11
+  (0.4ms) CREATE TABLE "books" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255) NOT NULL, "author" varchar(255) NOT NULL, "year" integer NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
12
+  (0.2ms) CREATE UNIQUE INDEX "index_books_on_title" ON "books" ("title")
13
+  (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES ('20121008115302')
14
+  (4.9ms) commit transaction
18
15
   (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
19
-  (0.0ms) PRAGMA index_list("books")
20
-  (0.0ms) PRAGMA index_info('index_books_on_title')
21
16
  Connecting to database specified by database.yml
22
-  (1.9ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
23
-  (0.4ms) select sqlite_version(*)
24
-  (3.8ms) CREATE TABLE "books" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255) NOT NULL, "author" varchar(255) NOT NULL, "year" integer NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
25
-  (0.0ms) PRAGMA index_list("books")
26
-  (1.2ms) CREATE UNIQUE INDEX "index_books_on_title" ON "books" ("title")
27
-  (1.2ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
28
-  (0.0ms) PRAGMA index_list("schema_migrations")
29
-  (1.0ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
17
+  (1.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
18
+  (0.2ms) select sqlite_version(*)
19
+  (1.0ms) CREATE TABLE "books" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255) NOT NULL, "author" varchar(255) NOT NULL, "year" integer NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
20
+  (0.8ms) CREATE UNIQUE INDEX "index_books_on_title" ON "books" ("title")
21
+  (0.8ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
22
+  (0.7ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
30
23
   (0.1ms) SELECT version FROM "schema_migrations"
31
24
   (0.8ms) INSERT INTO "schema_migrations" (version) VALUES ('20121008115302')
32
25
  Connecting to database specified by database.yml
33
-  (3.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
34
-  (1.1ms) select sqlite_version(*)
35
-  (1.6ms) DROP TABLE "books"
36
-  (1.3ms) CREATE TABLE "books" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255) NOT NULL, "author" varchar(255) NOT NULL, "year" integer NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
37
-  (1.2ms) CREATE UNIQUE INDEX "index_books_on_title" ON "books" ("title")
26
+  (1.4ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
27
+  (0.2ms) select sqlite_version(*)
28
+  (1.8ms) DROP TABLE "books"
29
+  (0.9ms) CREATE TABLE "books" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255) NOT NULL, "author" varchar(255) NOT NULL, "year" integer NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
30
+  (0.8ms) CREATE UNIQUE INDEX "index_books_on_title" ON "books" ("title")
38
31
   (0.1ms) SELECT version FROM "schema_migrations"
39
32
  Connecting to database specified by database.yml
40
-  (1.9ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
41
-  (0.4ms) select sqlite_version(*)
42
-  (3.5ms) DROP TABLE "books"
43
-  (1.2ms) CREATE TABLE "books" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255) NOT NULL, "author" varchar(255) NOT NULL, "year" integer NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
44
-  (1.0ms) CREATE UNIQUE INDEX "index_books_on_title" ON "books" ("title")
45
-  (0.1ms) SELECT version FROM "schema_migrations"
46
- Connecting to database specified by database.yml
47
-  (2.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
48
-  (0.4ms) select sqlite_version(*)
49
-  (3.3ms) DROP TABLE "books"
50
-  (1.0ms) CREATE TABLE "books" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255) NOT NULL, "author" varchar(255) NOT NULL, "year" integer NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
33
+  (1.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
34
+  (0.2ms) select sqlite_version(*)
35
+  (5.2ms) DROP TABLE "books"
36
+  (0.9ms) CREATE TABLE "books" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255) NOT NULL, "author" varchar(255) NOT NULL, "year" integer NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
51
37
   (0.9ms) CREATE UNIQUE INDEX "index_books_on_title" ON "books" ("title")
52
38
   (0.1ms) SELECT version FROM "schema_migrations"
53
39
  Connecting to database specified by database.yml
54
-  (3.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
55
-  (0.4ms) select sqlite_version(*)
56
-  (1.4ms) DROP TABLE "books"
40
+  (1.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
41
+  (0.2ms) select sqlite_version(*)
42
+  (5.5ms) DROP TABLE "books"
57
43
   (1.1ms) CREATE TABLE "books" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255) NOT NULL, "author" varchar(255) NOT NULL, "year" integer NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
58
-  (1.1ms) CREATE UNIQUE INDEX "index_books_on_title" ON "books" ("title")
44
+  (1.4ms) CREATE UNIQUE INDEX "index_books_on_title" ON "books" ("title")
45
+  (0.1ms) SELECT version FROM "schema_migrations"
46
+ Connecting to database specified by database.yml
47
+  (1.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
48
+  (0.2ms) select sqlite_version(*)
49
+  (1.3ms) DROP TABLE "books"
50
+  (0.8ms) CREATE TABLE "books" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255) NOT NULL, "author" varchar(255) NOT NULL, "year" integer NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
51
+  (0.7ms) CREATE UNIQUE INDEX "index_books_on_title" ON "books" ("title")
59
52
   (0.1ms) SELECT version FROM "schema_migrations"
@@ -1,28 +1,23 @@
1
1
  Connecting to database specified by database.yml
2
-  (2.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
2
+ Connecting to database specified by database.yml
3
+ Connecting to database specified by database.yml
4
+  (1.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
3
5
  Migrating to CreateBooks (20121008115302)
4
6
   (0.1ms) begin transaction
5
-  (0.1ms) rollback transaction
7
+  (0.0ms) rollback transaction
6
8
   (0.0ms) begin transaction
7
9
   (0.0ms) rollback transaction
8
10
  Connecting to database specified by database.yml
9
-  (2.5ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
11
+  (1.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
10
12
  Migrating to CreateBooks (20121008115302)
11
13
   (0.1ms) begin transaction
12
-  (0.1ms) rollback transaction
14
+  (0.0ms) rollback transaction
13
15
   (0.0ms) begin transaction
14
16
   (0.0ms) rollback transaction
15
17
  Connecting to database specified by database.yml
16
-  (1.9ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
18
+  (1.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
17
19
  Migrating to CreateBooks (20121008115302)
18
20
   (0.1ms) begin transaction
19
21
   (0.1ms) rollback transaction
20
-  (0.1ms) begin transaction
21
-  (0.0ms) rollback transaction
22
- Connecting to database specified by database.yml
23
-  (1.9ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
24
- Migrating to CreateBooks (20121008115302)
25
-  (0.1ms) begin transaction
26
-  (0.0ms) rollback transaction
27
22
   (0.0ms) begin transaction
28
-  (0.1ms) rollback transaction
23
+  (0.0ms) rollback transaction
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tableling-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17
4
+ version: 0.0.18
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-03 00:00:00.000000000 Z
12
+ date: 2013-07-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -41,8 +41,8 @@ files:
41
41
  - lib/tableling-rails/ext.rb
42
42
  - lib/tableling-rails/field.rb
43
43
  - lib/tableling-rails/global.rb
44
- - lib/tableling-rails/serializer.rb
45
44
  - lib/tableling-rails/settings.rb
45
+ - lib/tableling-rails/type_serializer.rb
46
46
  - lib/tableling-rails/version.rb
47
47
  - lib/tableling-rails/view.rb
48
48
  - lib/tableling-rails.rb
@@ -123,12 +123,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
123
  - - ! '>='
124
124
  - !ruby/object:Gem::Version
125
125
  version: '0'
126
+ segments:
127
+ - 0
128
+ hash: -4338373402448950759
126
129
  required_rubygems_version: !ruby/object:Gem::Requirement
127
130
  none: false
128
131
  requirements:
129
132
  - - ! '>='
130
133
  - !ruby/object:Gem::Version
131
134
  version: '0'
135
+ segments:
136
+ - 0
137
+ hash: -4338373402448950759
132
138
  requirements: []
133
139
  rubyforge_project:
134
140
  rubygems_version: 1.8.25