lolita 3.4.2 → 3.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +36 -36
  3. data/Gemfile +29 -29
  4. data/README.md +147 -147
  5. data/Rakefile +6 -6
  6. data/app/assets/javascripts/lolita/application.js +6 -6
  7. data/app/assets/javascripts/lolita/tab.js +100 -100
  8. data/app/assets/stylesheets/lolita/PIE-custom.htc +87 -87
  9. data/app/assets/stylesheets/lolita/PIE.htc +81 -81
  10. data/app/assets/stylesheets/lolita/application.css +6 -6
  11. data/app/assets/stylesheets/lolita/default.css.erb +169 -169
  12. data/app/assets/stylesheets/lolita/jquery-ui-1.8.16.lolita.css.erb +567 -567
  13. data/app/assets/stylesheets/lolita/style.css.erb +553 -553
  14. data/app/helpers/components/lolita/configuration/list_component.rb +10 -10
  15. data/app/helpers/components/lolita/configuration_component.rb +20 -20
  16. data/app/views/components/lolita/configuration/column/_first.html.haml +1 -1
  17. data/app/views/components/lolita/configuration/column/header/_first.html.haml +1 -1
  18. data/app/views/components/lolita/configuration/column/header/_sort.html.haml +6 -6
  19. data/app/views/components/lolita/configuration/columns/header/_display.html.haml +5 -5
  20. data/app/views/components/lolita/configuration/field/array/checkbox/_display.html.haml +7 -7
  21. data/app/views/components/lolita/configuration/field/string/text/_display.html.haml +7 -7
  22. data/app/views/components/lolita/configuration/search/_display.html.haml +2 -2
  23. data/app/views/components/lolita/configuration/tab/_display.html.haml +3 -3
  24. data/app/views/components/lolita/configuration/tabs/_form.html.haml +6 -6
  25. data/app/views/kaminari/lolita/_first_page.html.erb +11 -11
  26. data/app/views/kaminari/lolita/_gap.html.erb +8 -8
  27. data/app/views/kaminari/lolita/_last_page.html.erb +11 -11
  28. data/app/views/kaminari/lolita/_next_page.html.erb +11 -11
  29. data/app/views/kaminari/lolita/_page.html.erb +12 -12
  30. data/app/views/kaminari/lolita/_paginator.html.erb +23 -23
  31. data/app/views/kaminari/lolita/_prev_page.html.erb +11 -11
  32. data/app/views/lolita/info/index.html.erb +232 -232
  33. data/author +1 -1
  34. data/config/routes.rb +6 -6
  35. data/lib/generators/lolita/install_generator.rb +19 -19
  36. data/lib/generators/lolita/uninstall_generator.rb +70 -70
  37. data/lib/generators/templates/lolita.rb +13 -13
  38. data/lib/lolita.rb +140 -140
  39. data/lib/lolita/adapter/abstract_adapter.rb +15 -15
  40. data/lib/lolita/adapter/active_record.rb +227 -227
  41. data/lib/lolita/adapter/common_helper.rb +221 -221
  42. data/lib/lolita/adapter/field_helper.rb +18 -18
  43. data/lib/lolita/adapter/mongoid.rb +241 -241
  44. data/lib/lolita/components/base.rb +11 -11
  45. data/lib/lolita/configuration.rb +60 -60
  46. data/lib/lolita/configuration/column.rb +181 -181
  47. data/lib/lolita/configuration/field/big_decimal.rb +12 -12
  48. data/lib/lolita/configuration/field/boolean.rb +12 -12
  49. data/lib/lolita/configuration/field/date.rb +12 -12
  50. data/lib/lolita/configuration/field/hidden.rb +12 -12
  51. data/lib/lolita/configuration/field/integer.rb +11 -11
  52. data/lib/lolita/configuration/field/string.rb +16 -16
  53. data/lib/lolita/configuration/field/time.rb +13 -13
  54. data/lib/lolita/configuration/field_set.rb +25 -25
  55. data/lib/lolita/configuration/filter.rb +116 -116
  56. data/lib/lolita/configuration/list.rb +200 -200
  57. data/lib/lolita/configuration/search.rb +104 -104
  58. data/lib/lolita/controllers/component_helpers.rb +156 -156
  59. data/lib/lolita/controllers/internal_helpers.rb +71 -71
  60. data/lib/lolita/controllers/url_helpers.rb +7 -7
  61. data/lib/lolita/dbi/base.rb +56 -56
  62. data/lib/lolita/hooks/named_hook.rb +125 -125
  63. data/lib/lolita/lazy_loader.rb +54 -54
  64. data/lib/lolita/navigation/tree.rb +132 -132
  65. data/lib/lolita/rails/engine.rb +23 -23
  66. data/lib/lolita/rails/routes.rb +129 -129
  67. data/lib/lolita/ruby_ext/accessors.rb +26 -26
  68. data/lib/lolita/search/simple.rb +75 -75
  69. data/lib/lolita/support/formatter.rb +62 -62
  70. data/lib/lolita/support/formatter/rails.rb +56 -56
  71. data/lib/lolita/system_configuration/base.rb +178 -178
  72. data/lib/lolita/test/matchers.rb +77 -77
  73. data/lib/lolita/version.rb +30 -30
  74. data/lib/tasks/tinymce-assets.rake +6 -6
  75. data/lolita.gemspec +34 -34
  76. data/spec/adapter/common_helper_spec.rb +95 -95
  77. data/spec/adapter_helper.rb +42 -42
  78. data/spec/builder_spec.rb +120 -120
  79. data/spec/configuration/base_spec.rb +22 -22
  80. data/spec/configuration/field_spec.rb +118 -118
  81. data/spec/configuration/filter_spec.rb +131 -131
  82. data/spec/configuration/tab_spec.rb +187 -187
  83. data/spec/configuration/tabs_spec.rb +120 -120
  84. data/spec/generators/lolita/install_generator_spec.rb +54 -54
  85. data/spec/generators/lolita/uninstall_generator_spec.rb +48 -48
  86. data/spec/orm/mongoid.rb +12 -12
  87. data/spec/rails_app/app/controllers/application_controller.rb +3 -3
  88. data/spec/rails_app/app/helpers/application_helper.rb +3 -3
  89. data/spec/rails_app/app/mongoid/address.rb +7 -7
  90. data/spec/rails_app/app/mongoid/category.rb +18 -18
  91. data/spec/rails_app/app/mongoid/comment.rb +5 -5
  92. data/spec/rails_app/app/mongoid/post.rb +30 -30
  93. data/spec/rails_app/app/mongoid/preference.rb +5 -5
  94. data/spec/rails_app/app/mongoid/profile.rb +13 -13
  95. data/spec/rails_app/app/mongoid/tag.rb +3 -3
  96. data/spec/rails_app/app/views/components/lolita/configuration/list/_body_cell.html.erb +1 -1
  97. data/spec/rails_app/config/application.rb +33 -33
  98. data/spec/rails_app/config/boot.rb +7 -7
  99. data/spec/rails_app/config/environment.rb +5 -5
  100. data/spec/rails_app/config/environments/development.rb +23 -23
  101. data/spec/rails_app/config/environments/production.rb +37 -37
  102. data/spec/rails_app/config/environments/test.rb +37 -37
  103. data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -7
  104. data/spec/rails_app/config/initializers/inflections.rb +2 -2
  105. data/spec/rails_app/config/initializers/secret_token.rb +1 -1
  106. data/spec/rails_app/config/routes.rb +2 -2
  107. data/spec/rails_app/lib/lolita/configuration/field/my_custom_collection.rb +13 -13
  108. data/spec/rails_app/public/javascripts/jquery-1.5.1.min.js +15 -15
  109. data/spec/rails_app/public/javascripts/lolita/main.js +6 -6
  110. data/spec/rails_app/public/javascripts/modernizr-1.7.min.js +1 -1
  111. data/spec/rails_app/public/javascripts/rails.js +137 -137
  112. data/spec/rails_app/public/javascripts/tinymce/langs/en.js +221 -221
  113. data/spec/rails_app/public/javascripts/tinymce/license.txt +504 -504
  114. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/about.htm +52 -52
  115. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/anchor.htm +26 -26
  116. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/charmap.htm +51 -51
  117. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/color_picker.htm +74 -74
  118. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/editor_template_src.js +1328 -1328
  119. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/image.htm +80 -80
  120. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/js/about.js +73 -73
  121. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/js/anchor.js +42 -42
  122. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/js/charmap.js +354 -354
  123. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/js/color_picker.js +329 -329
  124. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/js/image.js +247 -247
  125. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/js/link.js +153 -153
  126. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/js/source_editor.js +56 -56
  127. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/langs/en.js +68 -68
  128. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/langs/en_dlg.js +53 -53
  129. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/link.htm +57 -57
  130. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/shortcuts.htm +47 -47
  131. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/skins/cirkuit/content.css +66 -66
  132. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/skins/cirkuit/dialog.css +117 -117
  133. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/skins/cirkuit/ui.css +988 -988
  134. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/source_editor.htm +25 -25
  135. data/spec/rails_app/public/javascripts/tinymce/tiny_mce_popup.js +4 -4
  136. data/spec/rails_app/public/stylesheets/lolita/default.css +169 -169
  137. data/spec/rails_app/public/stylesheets/lolita/style.css +214 -214
  138. data/spec/spec_helper.rb +51 -51
  139. data/vendor/assets/javascripts/application_vendor_lolita.js +4 -4
  140. data/vendor/assets/javascripts/jquery-numeric.js +279 -279
  141. data/vendor/assets/javascripts/modernizr_1_7_min.js +1 -1
  142. data/vendor/assets/javascripts/tinymce/themes/advanced/skins/cirkuit/content.css +66 -66
  143. data/vendor/assets/javascripts/tinymce/themes/advanced/skins/cirkuit/dialog.css +117 -117
  144. data/vendor/assets/javascripts/tinymce/themes/advanced/skins/cirkuit/ui.css +988 -988
  145. data/vendor/assets/stylesheets/jquery-ui-1.8.16.custom.css +567 -567
  146. metadata +4 -3
@@ -1,201 +1,201 @@
1
- module Lolita
2
- module Configuration
3
- class List < Lolita::Configuration::Base
4
- include Observable
5
- include Lolita::Builder
6
-
7
- attr_reader :initialized_attributes,:page_criteria
8
- attr_writer :title
9
-
10
- lolita_accessor :per_page, :pagination_method, :actions
11
-
12
- def initialize(dbp,*args,&block)
13
- set_and_validate_dbp(dbp)
14
- set_list_attributes do
15
- set_attributes(*args)
16
- self.instance_eval(&block) if block_given?
17
- end
18
- end
19
-
20
- def title(new_title = nil)
21
- if new_title
22
- @title = new_title
23
- end
24
- Lolita::Utils.dynamic_string(@title, :default => dbp.klass.lolita_model_name.human(:count => 2))
25
- end
26
-
27
- def action name, options = {}, &block
28
- @actions << decide_and_create_action(name, options, &block)
29
- @actions.flatten!
30
- end
31
-
32
- # Allow to crate nested list for list
33
- def list(*args, &block)
34
- if args && args.any? || block_given?
35
- association = dbi.associations[args[0].to_s.to_sym]
36
- association_dbi = association && Lolita::DBI::Base.create(association.klass)
37
- raise Lolita::UnknownDBPError.new("No DBI specified for list sublist") unless association_dbi
38
- Lolita::LazyLoader.lazy_load(self,:@list,Lolita::Configuration::NestedList,association_dbi, self, :association_name => association.name,&block)
39
- else
40
- @list
41
- end
42
- end
43
-
44
- # For details see Lolita::Configuration::Search
45
- def search *args, &block
46
- if (args && args.any?) || block_given?
47
- @search = Lolita::Configuration::Search.new(self.dbi,*args,&block)
48
- add_observer(@search)
49
- end
50
- @search
51
- end
52
-
53
- # Return page for list display. Method requires two arguments:
54
- # * <tt>current_page</tt> - number of current page
55
- # * <tt>request (optional) </tt> - request that is passed to adapter that passes this to model when #pagination_method is defined
56
- def paginate(current_page, request = nil)
57
- changed
58
- @page_criteria = dbi.paginate(current_page,@per_page,:request => request, :pagination_method => @pagination_method)
59
- notify_observers(:paginate,self,request)
60
- @page_criteria
61
- end
62
-
63
- # Set columns. Allowed classes are Lolita::Configuration::Columns or
64
- # Array.
65
- def columns=(possible_columns)
66
- if possible_columns.is_a?(Lolita::Configuration::Columns)
67
- @columns = possible_columns
68
- @columns.parent = self
69
- elsif possible_columns.respond_to?(:each)
70
- possible_columns.each{|possible_column|
71
- column(possible_column)
72
- }
73
- else
74
- raise ArgumentError.new("Accepts only Enumerable or Lolita::Configuration::Columns.")
75
- end
76
- end
77
-
78
- # Define columns for list. On first read if there is no columns they will be created.
79
- def columns(*args,&block)
80
- if (args && args.any?) || block_given? || !@columns
81
- self.columns = Lolita::Configuration::Columns.new(dbi,*args,&block)
82
- end
83
- @columns
84
- end
85
-
86
- # Block setter for columns
87
- def column(*args,&block)
88
- columns.column(*args, &block)
89
- end
90
-
91
- # checks if filter defined
92
- def filter?
93
- @filter.is_a?(Lolita::Configuration::Filter)
94
- end
95
-
96
- # Create or return filter
97
- def filter(*args,&block)
98
- if args && args.any? || block_given?
99
- @filter = Lolita::Configuration::Filter.new(dbi,*args,&block)
100
- add_observer(@filter)
101
- end
102
- @filter
103
- end
104
-
105
- def by_path(path)
106
- path = path.dup
107
- object = self
108
- while path.any?
109
- part = path.pop.match(/(l|c)_(\w+)/)
110
- object = if part[1] == "l"
111
- object.list
112
- else
113
- object.columns.by_name(part[2]).list
114
- end
115
- end
116
- object
117
- end
118
-
119
- private
120
-
121
- def set_list_attributes
122
- init_default_attributes
123
- yield if block_given?
124
- create_default_actions
125
- end
126
-
127
- def init_default_attributes
128
- initialize_actions
129
- @per_page = Lolita.application.per_page || 10
130
- end
131
-
132
- def create_default_actions
133
- if !skip_actions? && ( default_actions? || actions_empty?)
134
- initialize_actions
135
- @actions << add_edit_action
136
- @actions << add_destroy_action
137
- end
138
- end
139
-
140
- def initialize_actions
141
- @actions = [] unless @actions.respond_to?(:each)
142
- end
143
-
144
- def default_actions?
145
- actions.to_s.to_sym == :default
146
- end
147
-
148
- def included_default_actions?
149
- actions.include?(:default)
150
- end
151
-
152
- def actions_empty?
153
- (@actions.respond_to?(:each) && @actions.empty?)
154
- end
155
-
156
- def skip_actions?
157
- actions.to_s.to_sym == :none
158
- end
159
-
160
- def decide_and_create_action(name, options ={}, &block)
161
- if name.to_s == 'default'
162
- [add_edit_action,add_destroy_action]
163
- else
164
- create_action(name,options,&block)
165
- end
166
- end
167
-
168
- def create_action name, options = {}, &block
169
- Lolita::Configuration::Action.new(@dbi,name,options,&block)
170
- end
171
-
172
- def add_edit_action
173
- unless actions.detect{|existing_action| existing_action.name == :edit}
174
- create_action(:edit, &edit_action_block)
175
- end
176
- end
177
-
178
- def add_destroy_action
179
- unless actions.detect{|existing_action| existing_action.name == :destroy}
180
- create_action(:destroy, &destroy_action_block)
181
- end
182
- end
183
-
184
- def edit_action_block
185
- Proc.new do
186
- title Proc.new{::I18n.t("lolita.shared.edit")}
187
- url Proc.new{|view,record| view.send(:edit_lolita_resource_path, :id => record.id)}
188
- end
189
- end
190
-
191
- def destroy_action_block
192
- Proc.new do
193
- title Proc.new{::I18n.t("lolita.shared.delete")}
194
- url Proc.new{|view,record| view.send(:lolita_resource_path,:id => record.id)}
195
- html :method => :delete, :confirm => Proc.new{::I18n.t("lolita.list.confirm")}
196
- end
197
- end
198
-
199
- end
200
- end
1
+ module Lolita
2
+ module Configuration
3
+ class List < Lolita::Configuration::Base
4
+ include Observable
5
+ include Lolita::Builder
6
+
7
+ attr_reader :initialized_attributes,:page_criteria
8
+ attr_writer :title
9
+
10
+ lolita_accessor :per_page, :pagination_method, :actions
11
+
12
+ def initialize(dbp,*args,&block)
13
+ set_and_validate_dbp(dbp)
14
+ set_list_attributes do
15
+ set_attributes(*args)
16
+ self.instance_eval(&block) if block_given?
17
+ end
18
+ end
19
+
20
+ def title(new_title = nil)
21
+ if new_title
22
+ @title = new_title
23
+ end
24
+ Lolita::Utils.dynamic_string(@title, :default => dbp.klass.lolita_model_name.human(:count => 2))
25
+ end
26
+
27
+ def action name, options = {}, &block
28
+ @actions << decide_and_create_action(name, options, &block)
29
+ @actions.flatten!
30
+ end
31
+
32
+ # Allow to crate nested list for list
33
+ def list(*args, &block)
34
+ if args && args.any? || block_given?
35
+ association = dbi.associations[args[0].to_s.to_sym]
36
+ association_dbi = association && Lolita::DBI::Base.create(association.klass)
37
+ raise Lolita::UnknownDBPError.new("No DBI specified for list sublist") unless association_dbi
38
+ Lolita::LazyLoader.lazy_load(self,:@list,Lolita::Configuration::NestedList,association_dbi, self, :association_name => association.name,&block)
39
+ else
40
+ @list
41
+ end
42
+ end
43
+
44
+ # For details see Lolita::Configuration::Search
45
+ def search *args, &block
46
+ if (args && args.any?) || block_given?
47
+ @search = Lolita::Configuration::Search.new(self.dbi,*args,&block)
48
+ add_observer(@search)
49
+ end
50
+ @search
51
+ end
52
+
53
+ # Return page for list display. Method requires two arguments:
54
+ # * <tt>current_page</tt> - number of current page
55
+ # * <tt>request (optional) </tt> - request that is passed to adapter that passes this to model when #pagination_method is defined
56
+ def paginate(current_page, request = nil)
57
+ changed
58
+ @page_criteria = dbi.paginate(current_page,@per_page,:request => request, :pagination_method => @pagination_method)
59
+ notify_observers(:paginate,self,request)
60
+ @page_criteria
61
+ end
62
+
63
+ # Set columns. Allowed classes are Lolita::Configuration::Columns or
64
+ # Array.
65
+ def columns=(possible_columns)
66
+ if possible_columns.is_a?(Lolita::Configuration::Columns)
67
+ @columns = possible_columns
68
+ @columns.parent = self
69
+ elsif possible_columns.respond_to?(:each)
70
+ possible_columns.each{|possible_column|
71
+ column(possible_column)
72
+ }
73
+ else
74
+ raise ArgumentError.new("Accepts only Enumerable or Lolita::Configuration::Columns.")
75
+ end
76
+ end
77
+
78
+ # Define columns for list. On first read if there is no columns they will be created.
79
+ def columns(*args,&block)
80
+ if (args && args.any?) || block_given? || !@columns
81
+ self.columns = Lolita::Configuration::Columns.new(dbi,*args,&block)
82
+ end
83
+ @columns
84
+ end
85
+
86
+ # Block setter for columns
87
+ def column(*args,&block)
88
+ columns.column(*args, &block)
89
+ end
90
+
91
+ # checks if filter defined
92
+ def filter?
93
+ @filter.is_a?(Lolita::Configuration::Filter)
94
+ end
95
+
96
+ # Create or return filter
97
+ def filter(*args,&block)
98
+ if args && args.any? || block_given?
99
+ @filter = Lolita::Configuration::Filter.new(dbi,*args,&block)
100
+ add_observer(@filter)
101
+ end
102
+ @filter
103
+ end
104
+
105
+ def by_path(path)
106
+ path = path.dup
107
+ object = self
108
+ while path.any?
109
+ part = path.pop.match(/(l|c)_(\w+)/)
110
+ object = if part[1] == "l"
111
+ object.list
112
+ else
113
+ object.columns.by_name(part[2]).list
114
+ end
115
+ end
116
+ object
117
+ end
118
+
119
+ private
120
+
121
+ def set_list_attributes
122
+ init_default_attributes
123
+ yield if block_given?
124
+ create_default_actions
125
+ end
126
+
127
+ def init_default_attributes
128
+ initialize_actions
129
+ @per_page = Lolita.application.per_page || 10
130
+ end
131
+
132
+ def create_default_actions
133
+ if !skip_actions? && ( default_actions? || actions_empty?)
134
+ initialize_actions
135
+ @actions << add_edit_action
136
+ @actions << add_destroy_action
137
+ end
138
+ end
139
+
140
+ def initialize_actions
141
+ @actions = [] unless @actions.respond_to?(:each)
142
+ end
143
+
144
+ def default_actions?
145
+ actions.to_s.to_sym == :default
146
+ end
147
+
148
+ def included_default_actions?
149
+ actions.include?(:default)
150
+ end
151
+
152
+ def actions_empty?
153
+ (@actions.respond_to?(:each) && @actions.empty?)
154
+ end
155
+
156
+ def skip_actions?
157
+ actions.to_s.to_sym == :none
158
+ end
159
+
160
+ def decide_and_create_action(name, options ={}, &block)
161
+ if name.to_s == 'default'
162
+ [add_edit_action,add_destroy_action]
163
+ else
164
+ create_action(name,options,&block)
165
+ end
166
+ end
167
+
168
+ def create_action name, options = {}, &block
169
+ Lolita::Configuration::Action.new(@dbi,name,options,&block)
170
+ end
171
+
172
+ def add_edit_action
173
+ unless actions.detect{|existing_action| existing_action.name == :edit}
174
+ create_action(:edit, &edit_action_block)
175
+ end
176
+ end
177
+
178
+ def add_destroy_action
179
+ unless actions.detect{|existing_action| existing_action.name == :destroy}
180
+ create_action(:destroy, &destroy_action_block)
181
+ end
182
+ end
183
+
184
+ def edit_action_block
185
+ Proc.new do
186
+ title Proc.new{::I18n.t("lolita.shared.edit")}
187
+ url Proc.new{|view,record| view.send(:edit_lolita_resource_path, :id => record.id)}
188
+ end
189
+ end
190
+
191
+ def destroy_action_block
192
+ Proc.new do
193
+ title Proc.new{::I18n.t("lolita.shared.delete")}
194
+ url Proc.new{|view,record| view.send(:lolita_resource_path,:id => record.id)}
195
+ html :method => :delete, :confirm => Proc.new{::I18n.t("lolita.list.confirm")}
196
+ end
197
+ end
198
+
199
+ end
200
+ end
201
201
  end
@@ -1,105 +1,105 @@
1
- module Lolita
2
- module Configuration
3
- # Proxy class for search. It supports two methods #with and #run.
4
- # By default with method accepts method name or nothing and creates Lolita::Search::Simple instance
5
- # that will be used to run search on current dbi.
6
- # By default search run against all content fields, but when _:fields_ options is passed it search in
7
- # those fields only.
8
- # ==== Example
9
- # class Post < ActiveRecord::Base
10
- # include Lolita::Configuration
11
- # lolita do
12
- # list do
13
- # search :fields => [:name]
14
- # end
15
- # end
16
- # end
17
- # #with method also accepts class or instance of some class, that will be used to run search.
18
- # That class should have #run method that accepts query and request.
19
- # ====Example
20
- # class MyCustomSearch
21
- # def initialize(dbi)
22
- # @dbi = dbi
23
- # end
24
- #
25
- # def run(query,request = nil)
26
- # @dbi.klass.where(:my_field => query)
27
- # end
28
- # end
29
- # Also you can put your search method in model. For more see Lolita::Search::Simple
30
- # ====Example
31
- # class Post < ActiveRecord::Base
32
- # include Lolita::Configuration
33
- # lolita do
34
- # list do
35
- # search :my_custom_search
36
- # end
37
- # end
38
- #
39
- # def self.my_custom_search(query,request)
40
- # self.where(:title => query, :user_id => request.params[:user_id])
41
- # end
42
- # end
43
- class Search
44
- include Lolita::Builder
45
- attr_reader :dbi
46
- attr_writer :with, :fields
47
-
48
- def initialize dbi, *args, &block
49
- @dbi = dbi
50
- set_attributes(args ? args.extract_options! : {})
51
- instance_eval(&block) if block_given?
52
- @with ||= args[0]!=true && args[0] ? args[0] : nil
53
- end
54
-
55
- def with(value = nil)
56
- @with = value if value
57
- if !@with || [String,Symbol].include?(@with.class)
58
- @with = Lolita::Search::Simple.new(dbi,@with, :fields => @fields)
59
- elsif @with.class == Class
60
- initialize_arity = @with.instance_method(:initialize).arity
61
- @with = if initialize_arity < 0 || initialize_arity > 1
62
- @with.new(dbi,:fields => @fields)
63
- else
64
- @with.new(dbi)
65
- end
66
- end
67
- @with
68
- end
69
-
70
- def run(query,request = nil)
71
- if self.with.method(:run).arity < 0 || self.with.method(:run).arity > 1
72
- self.with.run(query,request)
73
- else
74
- self.with.run(query)
75
- end
76
- end
77
-
78
- def update method_name,list,request
79
- if method_name == :paginate && list.search
80
- search_criteria = list.search.run(request && request.params[:q] || "",request)
81
- page_criteria = if search_criteria.respond_to?(:where)
82
- list.page_criteria.merge(search_criteria)
83
- elsif search_criteria.nil?
84
- list.page_criteria
85
- else
86
- search_criteria
87
- end
88
- list.instance_variable_set(:@page_criteria,page_criteria)
89
- end
90
- end
91
-
92
- private
93
-
94
- def set_attributes options
95
- if options.respond_to?(:each)
96
- options.each do |method_name, value|
97
- self.send(:"#{method_name}=",value)
98
- end
99
- end
100
- end
101
-
102
- end
103
-
104
- end
1
+ module Lolita
2
+ module Configuration
3
+ # Proxy class for search. It supports two methods #with and #run.
4
+ # By default with method accepts method name or nothing and creates Lolita::Search::Simple instance
5
+ # that will be used to run search on current dbi.
6
+ # By default search run against all content fields, but when _:fields_ options is passed it search in
7
+ # those fields only.
8
+ # ==== Example
9
+ # class Post < ActiveRecord::Base
10
+ # include Lolita::Configuration
11
+ # lolita do
12
+ # list do
13
+ # search :fields => [:name]
14
+ # end
15
+ # end
16
+ # end
17
+ # #with method also accepts class or instance of some class, that will be used to run search.
18
+ # That class should have #run method that accepts query and request.
19
+ # ====Example
20
+ # class MyCustomSearch
21
+ # def initialize(dbi)
22
+ # @dbi = dbi
23
+ # end
24
+ #
25
+ # def run(query,request = nil)
26
+ # @dbi.klass.where(:my_field => query)
27
+ # end
28
+ # end
29
+ # Also you can put your search method in model. For more see Lolita::Search::Simple
30
+ # ====Example
31
+ # class Post < ActiveRecord::Base
32
+ # include Lolita::Configuration
33
+ # lolita do
34
+ # list do
35
+ # search :my_custom_search
36
+ # end
37
+ # end
38
+ #
39
+ # def self.my_custom_search(query,request)
40
+ # self.where(:title => query, :user_id => request.params[:user_id])
41
+ # end
42
+ # end
43
+ class Search
44
+ include Lolita::Builder
45
+ attr_reader :dbi
46
+ attr_writer :with, :fields
47
+
48
+ def initialize dbi, *args, &block
49
+ @dbi = dbi
50
+ set_attributes(args ? args.extract_options! : {})
51
+ instance_eval(&block) if block_given?
52
+ @with ||= args[0]!=true && args[0] ? args[0] : nil
53
+ end
54
+
55
+ def with(value = nil)
56
+ @with = value if value
57
+ if !@with || [String,Symbol].include?(@with.class)
58
+ @with = Lolita::Search::Simple.new(dbi,@with, :fields => @fields)
59
+ elsif @with.class == Class
60
+ initialize_arity = @with.instance_method(:initialize).arity
61
+ @with = if initialize_arity < 0 || initialize_arity > 1
62
+ @with.new(dbi,:fields => @fields)
63
+ else
64
+ @with.new(dbi)
65
+ end
66
+ end
67
+ @with
68
+ end
69
+
70
+ def run(query,request = nil)
71
+ if self.with.method(:run).arity < 0 || self.with.method(:run).arity > 1
72
+ self.with.run(query,request)
73
+ else
74
+ self.with.run(query)
75
+ end
76
+ end
77
+
78
+ def update method_name,list,request
79
+ if method_name == :paginate && list.search
80
+ search_criteria = list.search.run(request && request.params[:q] || "",request)
81
+ page_criteria = if search_criteria.respond_to?(:where)
82
+ list.page_criteria.merge(search_criteria)
83
+ elsif search_criteria.nil?
84
+ list.page_criteria
85
+ else
86
+ search_criteria
87
+ end
88
+ list.instance_variable_set(:@page_criteria,page_criteria)
89
+ end
90
+ end
91
+
92
+ private
93
+
94
+ def set_attributes options
95
+ if options.respond_to?(:each)
96
+ options.each do |method_name, value|
97
+ self.send(:"#{method_name}=",value)
98
+ end
99
+ end
100
+ end
101
+
102
+ end
103
+
104
+ end
105
105
  end