filtrum 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. checksums.yaml +15 -0
  2. data/LICENSE.txt +20 -0
  3. data/README.md +8 -0
  4. data/VERSION +1 -0
  5. data/lib/filtrum.rb +4 -0
  6. data/lib/filtrum/config.rb +33 -0
  7. data/lib/filtrum/engine.rb +4 -0
  8. data/lib/filtrum/errors.rb +5 -0
  9. data/lib/filtrum/ext.rb +13 -0
  10. data/lib/filtrum/field.rb +84 -0
  11. data/lib/filtrum/global.rb +10 -0
  12. data/lib/filtrum/settings.rb +33 -0
  13. data/lib/filtrum/type_serializer.rb +17 -0
  14. data/lib/filtrum/version.rb +3 -0
  15. data/lib/filtrum/view.rb +116 -0
  16. data/spec/dummy/README.rdoc +261 -0
  17. data/spec/dummy/Rakefile +7 -0
  18. data/spec/dummy/app/assets/javascripts/application.js +14 -0
  19. data/spec/dummy/app/assets/stylesheets/application.css.sass +17 -0
  20. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  21. data/spec/dummy/app/controllers/books_controller.rb +6 -0
  22. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  23. data/spec/dummy/app/helpers/books_helper.rb +2 -0
  24. data/spec/dummy/app/models/book.rb +17 -0
  25. data/spec/dummy/app/views/books/index.html.haml +1 -0
  26. data/spec/dummy/app/views/layouts/application.html.haml +16 -0
  27. data/spec/dummy/config.ru +4 -0
  28. data/spec/dummy/config/application.rb +53 -0
  29. data/spec/dummy/config/boot.rb +10 -0
  30. data/spec/dummy/config/database.yml +25 -0
  31. data/spec/dummy/config/environment.rb +5 -0
  32. data/spec/dummy/config/environments/development.rb +37 -0
  33. data/spec/dummy/config/environments/production.rb +73 -0
  34. data/spec/dummy/config/environments/test.rb +34 -0
  35. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  36. data/spec/dummy/config/initializers/inflections.rb +15 -0
  37. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  38. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  39. data/spec/dummy/config/initializers/session_store.rb +8 -0
  40. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  41. data/spec/dummy/config/locales/en.yml +5 -0
  42. data/spec/dummy/config/routes.rb +66 -0
  43. data/spec/dummy/db/development.sqlite3 +0 -0
  44. data/spec/dummy/db/migrate/20121008115302_create_books.rb +12 -0
  45. data/spec/dummy/db/schema.rb +26 -0
  46. data/spec/dummy/db/seeds.rb +73 -0
  47. data/spec/dummy/db/test.sqlite3 +0 -0
  48. data/spec/dummy/log/test.log +19 -0
  49. data/spec/dummy/public/404.html +26 -0
  50. data/spec/dummy/public/422.html +26 -0
  51. data/spec/dummy/public/500.html +25 -0
  52. data/spec/dummy/public/favicon.ico +0 -0
  53. data/spec/dummy/script/rails +6 -0
  54. data/spec/dummy/tmp/cache/assets/BF1/770/sprockets%2F8228a53c539c582499241527570aa216 +0 -0
  55. data/spec/dummy/tmp/cache/assets/C3F/0C0/sprockets%2F4643701467ac62c314510c8dd021916f +0 -0
  56. data/spec/dummy/tmp/cache/assets/C53/AD0/sprockets%2F712078516a81502575c831cb0a8e898e +0 -0
  57. data/spec/dummy/tmp/cache/assets/C59/0F0/sprockets%2F099f4e84a443568245ff0674030f6e05 +0 -0
  58. data/spec/dummy/tmp/cache/assets/C60/CF0/sprockets%2F32a45292ee67867d8715952f3072ad84 +0 -0
  59. data/spec/dummy/tmp/cache/assets/C6F/0E0/sprockets%2F82372776487d948ef7666d304d6fb345 +0 -0
  60. data/spec/dummy/tmp/cache/assets/C80/840/sprockets%2F562c2d168da585f80579347d10790a0a +0 -0
  61. data/spec/dummy/tmp/cache/assets/C82/F20/sprockets%2F117015d65237e3682f5b21c87d58ab95 +0 -0
  62. data/spec/dummy/tmp/cache/assets/C86/280/sprockets%2F1345c9947753f018f45371ec5083fa2d +0 -0
  63. data/spec/dummy/tmp/cache/assets/C91/350/sprockets%2F6864c49a10954375948d9f306db308fb +0 -0
  64. data/spec/dummy/tmp/cache/assets/C9A/900/sprockets%2Fc1b5b7611d7c10e124512724f29f241d +0 -0
  65. data/spec/dummy/tmp/cache/assets/CAF/540/sprockets%2F8f42e689b84f0b25c0180046467adf31 +0 -0
  66. data/spec/dummy/tmp/cache/assets/CB1/9A0/sprockets%2F57805dfa396248a9665cb4a0c85116d0 +0 -0
  67. data/spec/dummy/tmp/cache/assets/CC1/4E0/sprockets%2F6ef77414c793c84a437244d7d965ba90 +0 -0
  68. data/spec/dummy/tmp/cache/assets/CC3/B10/sprockets%2F0a2c59284c7a5d99581883a9763b5ad8 +0 -0
  69. data/spec/dummy/tmp/cache/assets/CC9/940/sprockets%2F8128c279185cf97dba7914278ce838b4 +0 -0
  70. data/spec/dummy/tmp/cache/assets/CCA/0C0/sprockets%2F6c3e053393afe303432ca73f1b41490b +0 -0
  71. data/spec/dummy/tmp/cache/assets/CD5/2C0/sprockets%2F166c056119ebdfb8b7104c97b424b423 +0 -0
  72. data/spec/dummy/tmp/cache/assets/CDB/610/sprockets%2F7adf20860b018477a95cdb8321b49d16 +0 -0
  73. data/spec/dummy/tmp/cache/assets/CDD/F50/sprockets%2F7353f31f80f1139aade47728a62af336 +0 -0
  74. data/spec/dummy/tmp/cache/assets/CE9/760/sprockets%2F5ae7fab601683996918936cad21fc244 +0 -0
  75. data/spec/dummy/tmp/cache/assets/CE9/B90/sprockets%2Fea2eb066933b1d8b5949972c3c79353a +0 -0
  76. data/spec/dummy/tmp/cache/assets/CF1/AF0/sprockets%2F14dc9633b61024231ebec0c1a4a0259f +0 -0
  77. data/spec/dummy/tmp/cache/assets/D02/340/sprockets%2Fae58ed66f72137a594b0912bab600e03 +0 -0
  78. data/spec/dummy/tmp/cache/assets/D0A/AC0/sprockets%2Fc154991d0a7564e15c0fb4a555c5c73d +0 -0
  79. data/spec/dummy/tmp/cache/assets/D0B/190/sprockets%2F68cb0d1054ca546fc473274c4ac8737c +0 -0
  80. data/spec/dummy/tmp/cache/assets/D11/530/sprockets%2F13bc8c66140adef2e97648630866aae3 +0 -0
  81. data/spec/dummy/tmp/cache/assets/D16/AA0/sprockets%2Ff181e659a47e2cf9c257f32d80452bd3 +0 -0
  82. data/spec/dummy/tmp/cache/assets/D18/840/sprockets%2F50c1885539f61359c48af74fcae1df31 +0 -0
  83. data/spec/dummy/tmp/cache/assets/D1B/940/sprockets%2F7c4819d1dd9b64a22941d8c7bd1e6954 +0 -0
  84. data/spec/dummy/tmp/cache/assets/D2B/1F0/sprockets%2F6a4c116e8316d082bb0ffae02e94b107 +0 -0
  85. data/spec/dummy/tmp/cache/assets/D3C/E20/sprockets%2Ffc6c44ec250bc532c55649e5292b5c1f +0 -0
  86. data/spec/dummy/tmp/cache/assets/D4B/C30/sprockets%2Fe8aa746e637d69ff0ac1e71e528c5292 +0 -0
  87. data/spec/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
  88. data/spec/dummy/tmp/cache/assets/D5A/E20/sprockets%2Febf23d670481ba70b341aa2eb6e37b08 +0 -0
  89. data/spec/dummy/tmp/cache/assets/D5C/1F0/sprockets%2F2d3f2cfd98bf787168fb59f84ab24784 +0 -0
  90. data/spec/dummy/tmp/cache/assets/D5D/C60/sprockets%2Ffa0336b42d01aa9173d9d4c12fc4e82d +0 -0
  91. data/spec/dummy/tmp/cache/assets/D65/DD0/sprockets%2Fed4503679ef66f0e597bfc738bc9e698 +0 -0
  92. data/spec/dummy/tmp/cache/assets/D68/930/sprockets%2F5a15e52e84502f9fceb7ea04020f65fc +0 -0
  93. data/spec/dummy/tmp/cache/assets/D8C/140/sprockets%2F2afdbb3902a1be5d2f045cfc837903a3 +0 -0
  94. data/spec/dummy/tmp/cache/assets/D8E/660/sprockets%2F3a3eed926bdf64000ccacb36022f78d4 +0 -0
  95. data/spec/dummy/tmp/cache/assets/D95/690/sprockets%2Fe4d19a04705ee5d72c7fa64dc2070ddb +0 -0
  96. data/spec/dummy/tmp/cache/assets/DA4/EC0/sprockets%2F8da2289e16c1d0cac94aa825ee18d99d +0 -0
  97. data/spec/dummy/tmp/cache/assets/DA6/120/sprockets%2Fc5880aca76ccbb51f9388362e8afc1e6 +0 -0
  98. data/spec/dummy/tmp/cache/assets/DA7/390/sprockets%2F25e744cd1b6f8f7ce1d52c1e86a8f19a +0 -0
  99. data/spec/dummy/tmp/cache/assets/DAB/3E0/sprockets%2F63d2fa621beec4fe878f9eb2884426ab +0 -0
  100. data/spec/dummy/tmp/cache/assets/DAB/660/sprockets%2F9a8c36769afcc7027b4b6ee0c3e77ce5 +0 -0
  101. data/spec/dummy/tmp/cache/assets/DBF/EB0/sprockets%2F2afcec323c8ba12a2b7c852f23aa589e +0 -0
  102. data/spec/dummy/tmp/cache/assets/DCE/9F0/sprockets%2F8fa8c08ea8da97c06cd6c32fe3e613b0 +0 -0
  103. data/spec/dummy/tmp/cache/assets/DCF/E50/sprockets%2Fc39ff2ed4e1fb4269c39d415acf0d90b +0 -0
  104. data/spec/dummy/tmp/cache/assets/DD8/0A0/sprockets%2Fb2c3097effcd6084ec75e1f745d1d7dd +0 -0
  105. data/spec/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
  106. data/spec/dummy/tmp/cache/assets/DF0/040/sprockets%2F04bbbe1ee5ca914173cd90cf6e8a4e0b +0 -0
  107. data/spec/dummy/tmp/cache/assets/DF7/C80/sprockets%2Ff503b3666caf04beb7cfe54cbdb0575e +0 -0
  108. data/spec/dummy/tmp/cache/assets/DFA/B70/sprockets%2F262fb95d3f7cfc16febb0f0128ac38ce +0 -0
  109. data/spec/dummy/tmp/cache/assets/E03/260/sprockets%2Fd1fcfea59ff53c32557ffcd93016cb2e +0 -0
  110. data/spec/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
  111. data/spec/dummy/tmp/cache/assets/E0A/510/sprockets%2F8efee416ba436e75bea85d4d367faea7 +0 -0
  112. data/spec/dummy/tmp/cache/assets/E43/810/sprockets%2F79b0ef75ceb26a9c8fca797db3efba96 +0 -0
  113. data/spec/dummy/tmp/cache/assets/E96/1B0/sprockets%2F6d5fe51f9c1a3d2f898becd0bcddef3e +0 -0
  114. data/spec/dummy/tmp/cache/assets/ED4/410/sprockets%2F082adbb8e2abd8a21c8b34aebbbfbfad +0 -0
  115. data/spec/dummy/tmp/pids/server.pid +1 -0
  116. data/spec/models/book_spec.rb +8 -0
  117. data/spec/spec_helper.rb +94 -0
  118. data/spec/version_spec.rb +8 -0
  119. metadata +278 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZWM1YzdlMjg3MjljNjM5YWMyOGFjODE5OTBkYWQ4NzcxZmYwNmYzMA==
5
+ data.tar.gz: !binary |-
6
+ YjYxOGQ3MjJhMDNmODE3MmQ5NmIzY2IxZTdjYzQ3OWYxNjEyZGQyMA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YzI2MDYxYjg0OWUzNzFmMjgwOWMzMWVhMDMxM2I0YTJiNzYxYzIyZTliZWI0
10
+ ZGYxNGQzOWNlMDcyZmI4YzcxMWRhNGQwZDY5MWJiZWI4ZmZiOTE3YjI1Yjg0
11
+ NGE0NWY5OWQzMWU2MjMwNmQ4NzJhNDRmZjkzNzc0OGUyMjFhMDM=
12
+ data.tar.gz: !binary |-
13
+ NjdjZGNiZjM2NDcwMjU5NDYyNDdiZTMzMGQxZjAzNjAxNWFiYjZmZTg3Mjdl
14
+ OTgzNDVkNDgwMmM4NTk2NGY1MTk1YzE0MDUzZDNlYjAzOWQ4OTc4MGEyZDNj
15
+ MWU0YmVmNjI4YTgwZjY2M2IzZjc2M2FiYTkzNGMyYjE1YTE0Y2M=
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012-2014 Simon Oulevay (Alpha Hydrae)
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.
@@ -0,0 +1,8 @@
1
+ # filtrum
2
+
3
+ **Active record filtering and serialization library.**
4
+
5
+ ## Meta
6
+
7
+ * **Author:** Simon Oulevay a.k.a. Alpha Hydrae
8
+ * **License:** MIT (see [LICENSE.txt](https://github.com/AlphaHydrae/filtrum/blob/master/LICENSE.txt))
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,4 @@
1
+ module Filtrum
2
+ end
3
+
4
+ Dir[File.join File.dirname(__FILE__), File.basename(__FILE__, '.*'), '*.rb'].each{ |lib| require lib }
@@ -0,0 +1,33 @@
1
+ module Filtrum
2
+
3
+ class Configuration
4
+ attr_reader :model, :settings
5
+
6
+ def initialize model, options = {}, &block
7
+
8
+ @model = model
9
+ @views = []
10
+
11
+ @settings = Settings.new Filtrum.settings
12
+ extend @settings.dsl
13
+
14
+ instance_eval &block if block
15
+ @frozen = true
16
+ end
17
+
18
+ def view name, options = {}, &block
19
+ return @views.find{ |v| v.name.to_s == name.to_s } if @frozen
20
+ @views.delete_if{ |v| v.name.to_s == name }
21
+ View.new(name, self, options, &block).tap{ |v| @views << v }
22
+ end
23
+
24
+ def default_view options = {}, &block
25
+ view :default, options, &block
26
+ end
27
+
28
+ def process params
29
+ raise ConfigurationError, "You must specify a default view" unless view(:default)
30
+ view(:default).process params
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,4 @@
1
+ module Filtrum
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,5 @@
1
+ module Filtrum
2
+ class Error < StandardError; end
3
+ class BaseQueryError < Error; end
4
+ class ConfigurationError < Error; end
5
+ end
@@ -0,0 +1,13 @@
1
+ module Filtrum
2
+
3
+ module Model
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+
8
+ def filtrum options = {}, &block
9
+ @filtrum ||= Filtrum::Configuration.new(self, options, &block)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,84 @@
1
+ module Filtrum
2
+
3
+ class Field
4
+ attr_accessor :name, :alias
5
+
6
+ def initialize name, view, options = {}, &block
7
+
8
+ @name, @view = name.to_s, view
9
+ @alias = options[:as].try :to_s
10
+ @value_column = options[:value].try :to_s
11
+ @includes = options[:includes]
12
+
13
+ if options[:order] == false
14
+ @no_order = true
15
+ elsif options[:order]
16
+ @order_column = options[:order].to_s
17
+ end
18
+
19
+ instance_eval &block if block
20
+ end
21
+
22
+ def working_name
23
+ @alias || @name
24
+ end
25
+
26
+ def as name
27
+ @alias = name.to_s
28
+ end
29
+
30
+ def order &block
31
+ @order_block = block
32
+ end
33
+
34
+ def no_order
35
+ @no_order = true
36
+ end
37
+
38
+ def value &block
39
+ @value_block = block
40
+ end
41
+
42
+ def includes &block
43
+ @includes_block = block
44
+ end
45
+
46
+ def with_order query, direction
47
+ return if @no_order
48
+ if @order_block
49
+ @order_block.call query, direction
50
+ else
51
+ query.order "#{model.table_name}.#{@order_column || @name} #{direction}"
52
+ end
53
+ end
54
+
55
+ def with_includes query
56
+ if @includes_block
57
+ @includes_block.call query
58
+ elsif @includes
59
+ query.includes @includes
60
+ else
61
+ query
62
+ end
63
+ end
64
+
65
+ def extract object
66
+ if @value_block
67
+ @value_block.call object
68
+ else
69
+ serialize object.send(@value_column || @name)
70
+ end
71
+ end
72
+
73
+ private
74
+
75
+ def model
76
+ @view.config.model
77
+ end
78
+
79
+ def serialize value
80
+ type_serializer = @view.settings.type_serializers.find{ |s| s.match? value }
81
+ type_serializer ? type_serializer.serialize(value) : value
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,10 @@
1
+ module Filtrum
2
+
3
+ def self.settings
4
+ @settings ||= Settings.new
5
+ end
6
+
7
+ def self.global &block
8
+ settings.configure &block
9
+ end
10
+ end
@@ -0,0 +1,33 @@
1
+ module Filtrum
2
+
3
+ class Settings
4
+
5
+ def initialize parent = nil
6
+ @parent = parent
7
+ @type_serializers = []
8
+ end
9
+
10
+ def configure &block
11
+ instance_eval &block if block
12
+ end
13
+
14
+ def serialize_type type, &block
15
+ @type_serializers << TypeSerializer.new(type, block)
16
+ end
17
+
18
+ def type_serializers
19
+ (@parent ? @parent.type_serializers : []) + @type_serializers
20
+ end
21
+
22
+ def dsl
23
+ m = Module.new do
24
+
25
+ def serialize_type type, &block
26
+ @settings.serialize_type type, &block
27
+ end
28
+ end
29
+ m.instance_variable_set :@settings, self
30
+ m
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,17 @@
1
+ module Filtrum
2
+
3
+ class TypeSerializer
4
+
5
+ def initialize type, block
6
+ @type, @block = type, block
7
+ end
8
+
9
+ def match? value
10
+ value.kind_of? @type
11
+ end
12
+
13
+ def serialize value
14
+ @block.call value
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Filtrum
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,116 @@
1
+ module Filtrum
2
+
3
+ class View
4
+ attr_reader :name, :config, :settings, :fields
5
+ attr_accessor :base_query, :base_count_query, :quick_search_block, :serialize_record_block, :serialize_response_block
6
+
7
+ def initialize name, config, options = {}, &block
8
+
9
+ @name, @config = name, config
10
+ @fields = []
11
+
12
+ @base_query = @config.model
13
+ @base_count_query = nil
14
+
15
+ @settings = Settings.new @config.settings
16
+
17
+ if block
18
+ dsl = DSL.new self
19
+ dsl.extend @settings.dsl
20
+ dsl.instance_eval &block
21
+ end
22
+ end
23
+
24
+ def field name
25
+ @fields.find{ |f| f.name.to_s == name.to_s }
26
+ end
27
+
28
+ def process options = {}
29
+
30
+ q = options[:base] || @base_query
31
+ cq = options[:base_count] || @base_count_query
32
+
33
+ if @quick_search_block and options[:quickSearch].present?
34
+ q = @quick_search_block.call q, options[:quickSearch].to_s
35
+ cq = @quick_search_block.call cq, options[:quickSearch].to_s if cq
36
+ end
37
+
38
+ total = (cq || q).count
39
+ raise BaseQueryError, "Count query must return a number" unless total.kind_of?(Fixnum)
40
+
41
+ if options[:sort].present?
42
+ options[:sort].select{ |item| item.match /\A([^ ]+)* (asc|desc)\Z/i }.each do |item|
43
+ parts = item.split ' '
44
+ # TODO: handle unknown fields
45
+ f = @fields.find{ |f| f.working_name.to_s == parts[0] }
46
+ q = f.with_order q, parts[1].downcase.to_sym if f
47
+ end
48
+ end
49
+
50
+ @fields.each{ |f| q = f.with_includes q }
51
+
52
+ response_options = (options[:response] || {}).merge({
53
+ total: total
54
+ })
55
+
56
+ page_size = options[:pageSize].to_i
57
+ page_size = 10 if page_size <= 0
58
+ q = q.limit page_size
59
+
60
+ max_page = (total.to_f / page_size).ceil
61
+ page = options[:page].to_i
62
+
63
+ # TODO: allow this to be disabled
64
+ if page < 1 or page > max_page
65
+ page = 1
66
+ response_options[:page] = 1
67
+ end
68
+
69
+ q = q.offset (page - 1) * page_size
70
+
71
+ serialize_response q, response_options
72
+ end
73
+
74
+ class DSL
75
+
76
+ def initialize view
77
+ @view = view
78
+ end
79
+
80
+ def field name, options = {}, &block
81
+ @view.fields.delete_if{ |f| f.name.to_s == name.to_s }
82
+ Field.new(name, @view, options, &block).tap{ |f| @view.fields << f }
83
+ end
84
+
85
+ %w(base base_count).each do |m|
86
+ define_method m do |q|
87
+ @view.send "#{m}_query=", q
88
+ end
89
+ end
90
+
91
+ %w(quick_search serialize_record serialize_response).each do |m|
92
+ define_method m do |&block|
93
+ @view.send "#{m}_block=", block
94
+ end
95
+ end
96
+ end
97
+
98
+ private
99
+
100
+ def serialize_response query, response_options = {}
101
+
102
+ res = response_options.merge data: query.to_a
103
+
104
+ if @serialize_response_block
105
+ @serialize_response_block.call res
106
+ else
107
+ res[:data] = res[:data].collect{ |r| serialize_record r }
108
+ res
109
+ end
110
+ end
111
+
112
+ def serialize_record record
113
+ @serialize_record_block ? @serialize_record_block.call(record) : @fields.inject({}){ |memo,f| memo[f.working_name] = f.extract(record); memo }
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,261 @@
1
+ == Welcome to Rails
2
+
3
+ Rails is a web-application framework that includes everything needed to create
4
+ database-backed web applications according to the Model-View-Control pattern.
5
+
6
+ This pattern splits the view (also called the presentation) into "dumb"
7
+ templates that are primarily responsible for inserting pre-built data in between
8
+ HTML tags. The model contains the "smart" domain objects (such as Account,
9
+ Product, Person, Post) that holds all the business logic and knows how to
10
+ persist themselves to a database. The controller handles the incoming requests
11
+ (such as Save New Account, Update Product, Show Post) by manipulating the model
12
+ and directing data to the view.
13
+
14
+ In Rails, the model is handled by what's called an object-relational mapping
15
+ layer entitled Active Record. This layer allows you to present the data from
16
+ database rows as objects and embellish these data objects with business logic
17
+ methods. You can read more about Active Record in
18
+ link:files/vendor/rails/activerecord/README.html.
19
+
20
+ The controller and view are handled by the Action Pack, which handles both
21
+ layers by its two parts: Action View and Action Controller. These two layers
22
+ are bundled in a single package due to their heavy interdependence. This is
23
+ unlike the relationship between the Active Record and Action Pack that is much
24
+ more separate. Each of these packages can be used independently outside of
25
+ Rails. You can read more about Action Pack in
26
+ link:files/vendor/rails/actionpack/README.html.
27
+
28
+
29
+ == Getting Started
30
+
31
+ 1. At the command prompt, create a new Rails application:
32
+ <tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
33
+
34
+ 2. Change directory to <tt>myapp</tt> and start the web server:
35
+ <tt>cd myapp; rails server</tt> (run with --help for options)
36
+
37
+ 3. Go to http://localhost:3000/ and you'll see:
38
+ "Welcome aboard: You're riding Ruby on Rails!"
39
+
40
+ 4. Follow the guidelines to start developing your application. You can find
41
+ the following resources handy:
42
+
43
+ * The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
44
+ * Ruby on Rails Tutorial Book: http://www.railstutorial.org/
45
+
46
+
47
+ == Debugging Rails
48
+
49
+ Sometimes your application goes wrong. Fortunately there are a lot of tools that
50
+ will help you debug it and get it back on the rails.
51
+
52
+ First area to check is the application log files. Have "tail -f" commands
53
+ running on the server.log and development.log. Rails will automatically display
54
+ debugging and runtime information to these files. Debugging info will also be
55
+ shown in the browser on requests from 127.0.0.1.
56
+
57
+ You can also log your own messages directly into the log file from your code
58
+ using the Ruby logger class from inside your controllers. Example:
59
+
60
+ class WeblogController < ActionController::Base
61
+ def destroy
62
+ @weblog = Weblog.find(params[:id])
63
+ @weblog.destroy
64
+ logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
65
+ end
66
+ end
67
+
68
+ The result will be a message in your log file along the lines of:
69
+
70
+ Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
71
+
72
+ More information on how to use the logger is at http://www.ruby-doc.org/core/
73
+
74
+ Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
75
+ several books available online as well:
76
+
77
+ * Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
78
+ * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
79
+
80
+ These two books will bring you up to speed on the Ruby language and also on
81
+ programming in general.
82
+
83
+
84
+ == Debugger
85
+
86
+ Debugger support is available through the debugger command when you start your
87
+ Mongrel or WEBrick server with --debugger. This means that you can break out of
88
+ execution at any point in the code, investigate and change the model, and then,
89
+ resume execution! You need to install ruby-debug to run the server in debugging
90
+ mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
91
+
92
+ class WeblogController < ActionController::Base
93
+ def index
94
+ @posts = Post.all
95
+ debugger
96
+ end
97
+ end
98
+
99
+ So the controller will accept the action, run the first line, then present you
100
+ with a IRB prompt in the server window. Here you can do things like:
101
+
102
+ >> @posts.inspect
103
+ => "[#<Post:0x14a6be8
104
+ @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
105
+ #<Post:0x14a6620
106
+ @attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
107
+ >> @posts.first.title = "hello from a debugger"
108
+ => "hello from a debugger"
109
+
110
+ ...and even better, you can examine how your runtime objects actually work:
111
+
112
+ >> f = @posts.first
113
+ => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
114
+ >> f.
115
+ Display all 152 possibilities? (y or n)
116
+
117
+ Finally, when you're ready to resume execution, you can enter "cont".
118
+
119
+
120
+ == Console
121
+
122
+ The console is a Ruby shell, which allows you to interact with your
123
+ application's domain model. Here you'll have all parts of the application
124
+ configured, just like it is when the application is running. You can inspect
125
+ domain models, change values, and save to the database. Starting the script
126
+ without arguments will launch it in the development environment.
127
+
128
+ To start the console, run <tt>rails console</tt> from the application
129
+ directory.
130
+
131
+ Options:
132
+
133
+ * Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
134
+ made to the database.
135
+ * Passing an environment name as an argument will load the corresponding
136
+ environment. Example: <tt>rails console production</tt>.
137
+
138
+ To reload your controllers and models after launching the console run
139
+ <tt>reload!</tt>
140
+
141
+ More information about irb can be found at:
142
+ link:http://www.rubycentral.org/pickaxe/irb.html
143
+
144
+
145
+ == dbconsole
146
+
147
+ You can go to the command line of your database directly through <tt>rails
148
+ dbconsole</tt>. You would be connected to the database with the credentials
149
+ defined in database.yml. Starting the script without arguments will connect you
150
+ to the development database. Passing an argument will connect you to a different
151
+ database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
152
+ PostgreSQL and SQLite 3.
153
+
154
+ == Description of Contents
155
+
156
+ The default directory structure of a generated Ruby on Rails application:
157
+
158
+ |-- app
159
+ | |-- assets
160
+ | |-- images
161
+ | |-- javascripts
162
+ | `-- stylesheets
163
+ | |-- controllers
164
+ | |-- helpers
165
+ | |-- mailers
166
+ | |-- models
167
+ | `-- views
168
+ | `-- layouts
169
+ |-- config
170
+ | |-- environments
171
+ | |-- initializers
172
+ | `-- locales
173
+ |-- db
174
+ |-- doc
175
+ |-- lib
176
+ | `-- tasks
177
+ |-- log
178
+ |-- public
179
+ |-- script
180
+ |-- test
181
+ | |-- fixtures
182
+ | |-- functional
183
+ | |-- integration
184
+ | |-- performance
185
+ | `-- unit
186
+ |-- tmp
187
+ | |-- cache
188
+ | |-- pids
189
+ | |-- sessions
190
+ | `-- sockets
191
+ `-- vendor
192
+ |-- assets
193
+ `-- stylesheets
194
+ `-- plugins
195
+
196
+ app
197
+ Holds all the code that's specific to this particular application.
198
+
199
+ app/assets
200
+ Contains subdirectories for images, stylesheets, and JavaScript files.
201
+
202
+ app/controllers
203
+ Holds controllers that should be named like weblogs_controller.rb for
204
+ automated URL mapping. All controllers should descend from
205
+ ApplicationController which itself descends from ActionController::Base.
206
+
207
+ app/models
208
+ Holds models that should be named like post.rb. Models descend from
209
+ ActiveRecord::Base by default.
210
+
211
+ app/views
212
+ Holds the template files for the view that should be named like
213
+ weblogs/index.html.erb for the WeblogsController#index action. All views use
214
+ eRuby syntax by default.
215
+
216
+ app/views/layouts
217
+ Holds the template files for layouts to be used with views. This models the
218
+ common header/footer method of wrapping views. In your views, define a layout
219
+ using the <tt>layout :default</tt> and create a file named default.html.erb.
220
+ Inside default.html.erb, call <% yield %> to render the view using this
221
+ layout.
222
+
223
+ app/helpers
224
+ Holds view helpers that should be named like weblogs_helper.rb. These are
225
+ generated for you automatically when using generators for controllers.
226
+ Helpers can be used to wrap functionality for your views into methods.
227
+
228
+ config
229
+ Configuration files for the Rails environment, the routing map, the database,
230
+ and other dependencies.
231
+
232
+ db
233
+ Contains the database schema in schema.rb. db/migrate contains all the
234
+ sequence of Migrations for your schema.
235
+
236
+ doc
237
+ This directory is where your application documentation will be stored when
238
+ generated using <tt>rake doc:app</tt>
239
+
240
+ lib
241
+ Application specific libraries. Basically, any kind of custom code that
242
+ doesn't belong under controllers, models, or helpers. This directory is in
243
+ the load path.
244
+
245
+ public
246
+ The directory available for the web server. Also contains the dispatchers and the
247
+ default HTML files. This should be set as the DOCUMENT_ROOT of your web
248
+ server.
249
+
250
+ script
251
+ Helper scripts for automation and generation.
252
+
253
+ test
254
+ Unit and functional tests along with fixtures. When using the rails generate
255
+ command, template test files will be generated for you and placed in this
256
+ directory.
257
+
258
+ vendor
259
+ External libraries that the application depends on. Also includes the plugins
260
+ subdirectory. If the app has frozen rails, those gems also go here, under
261
+ vendor/rails/. This directory is in the load path.