lolita 3.3.4 → 3.3.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/stylesheets/lolita/PIE-custom.htc +87 -87
  3. data/app/assets/stylesheets/lolita/PIE.htc +81 -81
  4. data/app/assets/stylesheets/lolita/application.css +6 -6
  5. data/app/assets/stylesheets/lolita/default.css.erb +169 -169
  6. data/app/assets/stylesheets/lolita/jquery-ui-1.8.16.lolita.css.erb +567 -567
  7. data/app/controllers/lolita/field_data_controller.rb~ +45 -0
  8. data/app/helpers/components/lolita/configuration/list_component.rb +10 -10
  9. data/app/views/components/lolita/configuration/column/_first.html.haml +1 -1
  10. data/app/views/components/lolita/configuration/column/header/_first.html.haml +1 -1
  11. data/app/views/components/lolita/configuration/column/header/_sort.html.haml +6 -6
  12. data/app/views/components/lolita/configuration/columns/header/_display.html.haml +5 -5
  13. data/app/views/components/lolita/configuration/field/array/autocomplete/_display.html.haml~ +16 -0
  14. data/app/views/components/lolita/configuration/field/array/checkbox/_display.html.haml +7 -7
  15. data/app/views/components/lolita/configuration/filter/_display.html.haml +2 -2
  16. data/app/views/components/lolita/configuration/filter/_display.html.haml~ +9 -0
  17. data/app/views/components/lolita/configuration/search/_display.html.haml +2 -2
  18. data/app/views/kaminari/lolita/_first_page.html.erb +11 -11
  19. data/app/views/kaminari/lolita/_gap.html.erb +8 -8
  20. data/app/views/kaminari/lolita/_last_page.html.erb +11 -11
  21. data/app/views/kaminari/lolita/_next_page.html.erb +11 -11
  22. data/app/views/kaminari/lolita/_page.html.erb +12 -12
  23. data/app/views/kaminari/lolita/_paginator.html.erb +23 -23
  24. data/app/views/kaminari/lolita/_prev_page.html.erb +11 -11
  25. data/app/views/lolita/info/index.html.erb +232 -232
  26. data/lib/lolita/adapter/field_helper.rb +18 -18
  27. data/lib/lolita/configuration/field/big_decimal.rb +12 -12
  28. data/lib/lolita/configuration/field/boolean.rb +12 -12
  29. data/lib/lolita/configuration/field/date.rb +12 -12
  30. data/lib/lolita/configuration/field/hidden.rb +12 -12
  31. data/lib/lolita/configuration/field/integer.rb +11 -11
  32. data/lib/lolita/configuration/field/string.rb +16 -16
  33. data/lib/lolita/configuration/field/time.rb +13 -13
  34. data/lib/lolita/configuration/field_set.rb +25 -25
  35. data/lib/lolita/configuration/filter.rb +1 -7
  36. data/lib/lolita/configuration/filter.rb~ +122 -0
  37. data/lib/lolita/configuration/nested_form.rb +104 -104
  38. data/lib/lolita/configuration/search.rb +104 -104
  39. data/lib/lolita/dbi/base.rb +56 -56
  40. data/lib/lolita/hooks/named_hook.rb +125 -125
  41. data/lib/lolita/lazy_loader.rb +54 -54
  42. data/lib/lolita/navigation/tree.rb +132 -132
  43. data/lib/lolita/ruby_ext/accessors.rb +26 -26
  44. data/lib/lolita/search/simple.rb +75 -75
  45. data/lib/lolita/support/formatter.rb +62 -62
  46. data/lib/lolita/support/formatter/rails.rb +56 -56
  47. data/lib/lolita/test/matchers.rb +77 -77
  48. data/lib/lolita/version.rb +1 -1
  49. data/lib/lolita/version.rb~ +30 -0
  50. data/spec/builder_spec.rb +120 -120
  51. data/spec/configuration/filter_spec.rb +1 -30
  52. data/spec/configuration/filter_spec.rb~ +167 -0
  53. data/spec/rails_app/app/controllers/application_controller.rb +3 -3
  54. data/spec/rails_app/app/helpers/application_helper.rb +3 -3
  55. data/spec/rails_app/app/mongoid/address.rb +7 -7
  56. data/spec/rails_app/app/mongoid/comment.rb +5 -5
  57. data/spec/rails_app/app/mongoid/preference.rb +5 -5
  58. data/spec/rails_app/app/mongoid/profile.rb +13 -13
  59. data/spec/rails_app/app/mongoid/tag.rb +3 -3
  60. data/spec/rails_app/app/views/components/lolita/configuration/list/_body_cell.html.erb +1 -1
  61. data/spec/rails_app/config/environment.rb +5 -5
  62. data/spec/rails_app/config/environments/production.rb +37 -37
  63. data/spec/rails_app/config/environments/test.rb +37 -37
  64. data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -7
  65. data/spec/rails_app/config/initializers/inflections.rb +2 -2
  66. data/spec/rails_app/config/initializers/secret_token.rb +1 -1
  67. data/spec/rails_app/lib/lolita/configuration/field/my_custom_collection.rb +13 -13
  68. data/spec/rails_app/log/development.log +753 -14016
  69. data/spec/rails_app/public/javascripts/jquery-1.5.1.min.js +15 -15
  70. data/spec/rails_app/public/javascripts/lolita/main.js +6 -6
  71. data/spec/rails_app/public/javascripts/modernizr-1.7.min.js +1 -1
  72. data/spec/rails_app/public/javascripts/rails.js +137 -137
  73. data/spec/rails_app/public/javascripts/tinymce/langs/en.js +221 -221
  74. data/spec/rails_app/public/javascripts/tinymce/license.txt +504 -504
  75. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/about.htm +52 -52
  76. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/anchor.htm +26 -26
  77. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/charmap.htm +51 -51
  78. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/color_picker.htm +74 -74
  79. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/editor_template_src.js +1328 -1328
  80. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/image.htm +80 -80
  81. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/js/about.js +73 -73
  82. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/js/anchor.js +42 -42
  83. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/js/charmap.js +354 -354
  84. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/js/color_picker.js +329 -329
  85. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/js/image.js +247 -247
  86. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/js/link.js +153 -153
  87. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/js/source_editor.js +56 -56
  88. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/langs/en.js +68 -68
  89. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/langs/en_dlg.js +53 -53
  90. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/link.htm +57 -57
  91. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/shortcuts.htm +47 -47
  92. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/skins/cirkuit/content.css +66 -66
  93. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/skins/cirkuit/dialog.css +117 -117
  94. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/skins/cirkuit/ui.css +988 -988
  95. data/spec/rails_app/public/javascripts/tinymce/themes/advanced/source_editor.htm +25 -25
  96. data/spec/rails_app/public/javascripts/tinymce/tiny_mce_popup.js +4 -4
  97. data/spec/rails_app/public/stylesheets/lolita/default.css +169 -169
  98. data/spec/rails_app/public/stylesheets/lolita/style.css +214 -214
  99. data/vendor/assets/javascripts/jquery-numeric.js +279 -279
  100. data/vendor/assets/javascripts/modernizr_1_7_min.js +1 -1
  101. data/vendor/assets/javascripts/tinymce/themes/advanced/skins/cirkuit/content.css +66 -66
  102. data/vendor/assets/javascripts/tinymce/themes/advanced/skins/cirkuit/dialog.css +117 -117
  103. data/vendor/assets/javascripts/tinymce/themes/advanced/skins/cirkuit/ui.css +988 -988
  104. data/vendor/assets/stylesheets/jquery-ui-1.8.16.custom.css +567 -567
  105. metadata +347 -340
@@ -1,55 +1,55 @@
1
- module Lolita
2
- class LazyLoader
3
-
4
- #attr_reader :lazy_class,:eval_block,:class_instance
5
-
6
- def self.lazy_load(instance_name,var_name,lazy_class,*args,&block)
7
- temp_var = instance_name.instance_variable_get(var_name)
8
- is_loader = temp_var.to_s =~ /Lolita::LazyLoader/
9
- if !temp_var || ((args && args.any? && is_loader && temp_var.__is_args_diff__(args)) || block_given?)
10
- temp_var = instance_name.instance_variable_set(var_name,self.new(lazy_class,*args,&block))
11
- end
12
- if temp_var.to_s =~ /Lolita::LazyLoader/
13
- temp_var = instance_name.instance_variable_set(var_name,temp_var.class_instance)
14
- end
15
- temp_var
16
- end
17
-
18
- def __is_args_diff__(args)
19
- @args != args
20
- end
21
-
22
- def initialize(lazy_class,*args,&block)
23
- @args = args || []
24
- @lazy_class=lazy_class
25
- @eval_block=block
26
- end
27
-
28
- def class_instance
29
- @class_instance || self
30
- end
31
-
32
- def method_missing(method_name,*args,&block)
33
- unless @class_instance
34
- @args = @args.empty? && [nil] || @args
35
- arity=@lazy_class.instance_method(:initialize).arity
36
- if arity==-1 # when expectign *args
37
- @class_instance=@lazy_class.new(*@args,&@eval_block)
38
- elsif arity.abs>0 # when expecting specific number of arguments without any *args
39
- @class_instance=@lazy_class.new(*@args.slice(0..arity.abs-1),&@eval_block)
40
- else
41
- @class_instance=@lazy_class.new(&@eval_block)
42
- end
43
- end
44
- if @class_instance.respond_to?(:after_initialize,true)
45
- @class_instance.__send__(:after_initialize)
46
- end
47
- @class_instance.__send__(method_name,*args,&block)
48
- end
49
-
50
- instance_methods.each { |method|
51
- next if ["!","__is_args_diff__","hash","respond_to?","__id__","__send__","to_s","object_id","method_missing","class_instance","initialize", "__args__"].include?(method.to_s)
52
- eval("undef :#{method}")
53
- }
54
- end
1
+ module Lolita
2
+ class LazyLoader
3
+
4
+ #attr_reader :lazy_class,:eval_block,:class_instance
5
+
6
+ def self.lazy_load(instance_name,var_name,lazy_class,*args,&block)
7
+ temp_var = instance_name.instance_variable_get(var_name)
8
+ is_loader = temp_var.to_s =~ /Lolita::LazyLoader/
9
+ if !temp_var || ((args && args.any? && is_loader && temp_var.__is_args_diff__(args)) || block_given?)
10
+ temp_var = instance_name.instance_variable_set(var_name,self.new(lazy_class,*args,&block))
11
+ end
12
+ if temp_var.to_s =~ /Lolita::LazyLoader/
13
+ temp_var = instance_name.instance_variable_set(var_name,temp_var.class_instance)
14
+ end
15
+ temp_var
16
+ end
17
+
18
+ def __is_args_diff__(args)
19
+ @args != args
20
+ end
21
+
22
+ def initialize(lazy_class,*args,&block)
23
+ @args = args || []
24
+ @lazy_class=lazy_class
25
+ @eval_block=block
26
+ end
27
+
28
+ def class_instance
29
+ @class_instance || self
30
+ end
31
+
32
+ def method_missing(method_name,*args,&block)
33
+ unless @class_instance
34
+ @args = @args.empty? && [nil] || @args
35
+ arity=@lazy_class.instance_method(:initialize).arity
36
+ if arity==-1 # when expectign *args
37
+ @class_instance=@lazy_class.new(*@args,&@eval_block)
38
+ elsif arity.abs>0 # when expecting specific number of arguments without any *args
39
+ @class_instance=@lazy_class.new(*@args.slice(0..arity.abs-1),&@eval_block)
40
+ else
41
+ @class_instance=@lazy_class.new(&@eval_block)
42
+ end
43
+ end
44
+ if @class_instance.respond_to?(:after_initialize,true)
45
+ @class_instance.__send__(:after_initialize)
46
+ end
47
+ @class_instance.__send__(method_name,*args,&block)
48
+ end
49
+
50
+ instance_methods.each { |method|
51
+ next if ["!","__is_args_diff__","hash","respond_to?","__id__","__send__","to_s","object_id","method_missing","class_instance","initialize", "__args__"].include?(method.to_s)
52
+ eval("undef :#{method}")
53
+ }
54
+ end
55
55
  end
@@ -1,133 +1,133 @@
1
- module Lolita
2
- module Navigation
3
- class Tree
4
- include Enumerable
5
- include Lolita::Hooks
6
-
7
- class<<self
8
- def remember(tree)
9
- @@trees||={}
10
- @@trees[tree.name.to_sym]=tree
11
- end
12
-
13
- def [](name)
14
- @@trees||={}
15
- @@trees[name]
16
- end
17
- end
18
-
19
- add_hooks :before_branch_added, :after_branch_added
20
-
21
- attr_reader :name,:root,:default_position,:branches,:parent
22
-
23
- def initialize(name)
24
- @name=name
25
- @default_possition=:append
26
- @branches=[]
27
- end
28
-
29
- def each
30
- @branches.each do |branch|
31
- yield branch
32
- if branch.children.any?
33
- branch.children.each do |child|
34
- yield child
35
- end
36
- end
37
- end
38
- end
39
-
40
- def root?
41
- !parent
42
- end
43
-
44
- def visible?(view)
45
- self.branches.inject([]){|result,branch|
46
- if branch.visible?(view)
47
- result << true
48
- else
49
- result
50
- end
51
- }.any?
52
- end
53
-
54
- def populate_urls_in_branches(view)
55
- self.each do |branch|
56
- branch.populate_url(view)
57
- end
58
- end
59
-
60
- def method_missing method_name, *args
61
- @branches.send(method_name.to_sym,*args)
62
- end
63
-
64
-
65
- def append(*branch)
66
- adding_branch(*branch) do |fixed_branch|
67
- @branches<<fixed_branch
68
- end
69
- end
70
-
71
- def prepend(*branch)
72
- adding_branch(*branch) do |fixed_branch|
73
- @branches.unshift(fixed_branch)
74
- end
75
- end
76
-
77
- def after(given_branch,*other_branch)
78
- index=get_branch_index(given_branch)
79
-
80
- adding_branch(*other_branch) do |fixed_branch|
81
- put_in_branches(fixed_branch,index)
82
- end
83
- end
84
-
85
- def before(given_branch,*other_branch)
86
- index=get_branch_index(given_branch)
87
-
88
- adding_branch(*other_branch) do |fixed_branch|
89
- put_in_branches(fixed_branch,index-1)
90
- end
91
- end
92
-
93
- def get_branch_index(given_branch)
94
- @branches.each_with_index{|branch,index|
95
- return index if given_branch==branch
96
- }
97
- raise ArgumentError, "Branch #{given_branch.inspect} not exists in #{self.inspect}"
98
- end
99
-
100
- def set_parent(new_parent)
101
- @parent=new_parent
102
- end
103
-
104
- private
105
-
106
- def adding_branch *branch
107
- self.run(:before_branch_added,*branch)
108
- fixed_branch=fix_branch(*branch)
109
- yield fixed_branch
110
- @last_branch=fixed_branch
111
- self.run(:after_branch_added,fixed_branch)
112
- fixed_branch
113
- end
114
-
115
- def fix_branch(*branch)
116
- unless branch[0].is_a?(Lolita::Navigation::Branch)
117
- options=branch.extract_options!
118
- Lolita::Navigation::Branch.get_or_create(*branch,options.merge(:tree=>self))
119
- else
120
- branch[0].tree=self
121
- branch[0]
122
- end
123
- end
124
-
125
- def put_in_branches branch,index
126
- before_part=@branches.slice(0,index+1) || []
127
- after_part=@branches.slice(index+1,@branches.size-index) || []
128
- @branches=before_part+[branch]+after_part
129
- end
130
-
131
- end
132
- end
1
+ module Lolita
2
+ module Navigation
3
+ class Tree
4
+ include Enumerable
5
+ include Lolita::Hooks
6
+
7
+ class<<self
8
+ def remember(tree)
9
+ @@trees||={}
10
+ @@trees[tree.name.to_sym]=tree
11
+ end
12
+
13
+ def [](name)
14
+ @@trees||={}
15
+ @@trees[name]
16
+ end
17
+ end
18
+
19
+ add_hooks :before_branch_added, :after_branch_added
20
+
21
+ attr_reader :name,:root,:default_position,:branches,:parent
22
+
23
+ def initialize(name)
24
+ @name=name
25
+ @default_possition=:append
26
+ @branches=[]
27
+ end
28
+
29
+ def each
30
+ @branches.each do |branch|
31
+ yield branch
32
+ if branch.children.any?
33
+ branch.children.each do |child|
34
+ yield child
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ def root?
41
+ !parent
42
+ end
43
+
44
+ def visible?(view)
45
+ self.branches.inject([]){|result,branch|
46
+ if branch.visible?(view)
47
+ result << true
48
+ else
49
+ result
50
+ end
51
+ }.any?
52
+ end
53
+
54
+ def populate_urls_in_branches(view)
55
+ self.each do |branch|
56
+ branch.populate_url(view)
57
+ end
58
+ end
59
+
60
+ def method_missing method_name, *args
61
+ @branches.send(method_name.to_sym,*args)
62
+ end
63
+
64
+
65
+ def append(*branch)
66
+ adding_branch(*branch) do |fixed_branch|
67
+ @branches<<fixed_branch
68
+ end
69
+ end
70
+
71
+ def prepend(*branch)
72
+ adding_branch(*branch) do |fixed_branch|
73
+ @branches.unshift(fixed_branch)
74
+ end
75
+ end
76
+
77
+ def after(given_branch,*other_branch)
78
+ index=get_branch_index(given_branch)
79
+
80
+ adding_branch(*other_branch) do |fixed_branch|
81
+ put_in_branches(fixed_branch,index)
82
+ end
83
+ end
84
+
85
+ def before(given_branch,*other_branch)
86
+ index=get_branch_index(given_branch)
87
+
88
+ adding_branch(*other_branch) do |fixed_branch|
89
+ put_in_branches(fixed_branch,index-1)
90
+ end
91
+ end
92
+
93
+ def get_branch_index(given_branch)
94
+ @branches.each_with_index{|branch,index|
95
+ return index if given_branch==branch
96
+ }
97
+ raise ArgumentError, "Branch #{given_branch.inspect} not exists in #{self.inspect}"
98
+ end
99
+
100
+ def set_parent(new_parent)
101
+ @parent=new_parent
102
+ end
103
+
104
+ private
105
+
106
+ def adding_branch *branch
107
+ self.run(:before_branch_added,*branch)
108
+ fixed_branch=fix_branch(*branch)
109
+ yield fixed_branch
110
+ @last_branch=fixed_branch
111
+ self.run(:after_branch_added,fixed_branch)
112
+ fixed_branch
113
+ end
114
+
115
+ def fix_branch(*branch)
116
+ unless branch[0].is_a?(Lolita::Navigation::Branch)
117
+ options=branch.extract_options!
118
+ Lolita::Navigation::Branch.get_or_create(*branch,options.merge(:tree=>self))
119
+ else
120
+ branch[0].tree=self
121
+ branch[0]
122
+ end
123
+ end
124
+
125
+ def put_in_branches branch,index
126
+ before_part=@branches.slice(0,index+1) || []
127
+ after_part=@branches.slice(index+1,@branches.size-index) || []
128
+ @branches=before_part+[branch]+after_part
129
+ end
130
+
131
+ end
132
+ end
133
133
  end
@@ -1,27 +1,27 @@
1
- class Object
2
- # Works similar as <code>attr_accessor</code> only reader method is changed
3
- # to allow to set value (used for Lolita blocks in different classes).
4
- # ====Example
5
- # class Klass
6
- # lolita_accessor :my_method
7
- # end
8
- # k=Klass.new
9
- # k.my_method("it's me")
10
- # puts k.my_method #=> it's me
11
- def lolita_accessor *methods
12
- if [Class,Module].include?(self.class)
13
- methods.each do |method|
14
- class_eval <<-ACCESSORS,__FILE__,__LINE__+1
15
- def #{method}(value=nil)
16
- @#{method}=value if value
17
- @#{method}
18
- end
19
-
20
- def #{method}=(value)
21
- @#{method}=value
22
- end
23
- ACCESSORS
24
- end
25
- end
26
- end
1
+ class Object
2
+ # Works similar as <code>attr_accessor</code> only reader method is changed
3
+ # to allow to set value (used for Lolita blocks in different classes).
4
+ # ====Example
5
+ # class Klass
6
+ # lolita_accessor :my_method
7
+ # end
8
+ # k=Klass.new
9
+ # k.my_method("it's me")
10
+ # puts k.my_method #=> it's me
11
+ def lolita_accessor *methods
12
+ if [Class,Module].include?(self.class)
13
+ methods.each do |method|
14
+ class_eval <<-ACCESSORS,__FILE__,__LINE__+1
15
+ def #{method}(value=nil)
16
+ @#{method}=value if value
17
+ @#{method}
18
+ end
19
+
20
+ def #{method}=(value)
21
+ @#{method}=value
22
+ end
23
+ ACCESSORS
24
+ end
25
+ end
26
+ end
27
27
  end
@@ -1,75 +1,75 @@
1
- module Lolita
2
- module Search
3
-
4
- # Default search class for Lolita::Search. Lolita::Configuration::Search uses this by default.
5
- # It accepts method name as constructor argument, when none is given it call Lolita::DBI#search.
6
- class Simple
7
-
8
- # Method in model used to run a search.
9
- attr_accessor :search_method
10
- attr_reader :dbi
11
-
12
- # Accepts search method as optional argument
13
- def initialize(dbi, *args)
14
- @dbi = dbi
15
- @options = args.extract_options!
16
- @search_method = args[0]
17
- end
18
-
19
- # Require dbi (Lolita::DBI instance), query (String) and request and dbi as optional argument.
20
- # Also you can pass options.
21
- # ====Example
22
- # search.run("query",:fields => [:name])
23
- # # this will search only in :name field
24
- # search.run("query",nil, Lolita::DBI::Base.create(Category))
25
- # # this will use Category dbi for search
26
- # When there is search method defined, it uses that otherwise run default search.
27
- def run(query,*args)
28
- with_query(query,*args) do
29
- if self.search_method
30
- run_custom_search
31
- else
32
- run_default_search
33
- end
34
- end
35
- end
36
-
37
- private
38
-
39
- def with_query(query,*args)
40
- begin
41
- options = args.extract_options!
42
- @old_dbi = self.dbi
43
- @old_options = @options
44
- @options = options if options.any?
45
- @dbi = args[1] if args[1]
46
- @query = query
47
- @request = args[0]
48
- yield
49
- ensure
50
- @dbi = @old_dbi
51
- @options = @old_options
52
- @query,@request = nil,nil
53
- end
54
- end
55
-
56
- def run_custom_search
57
- search_method_arity = @dbi.klass.method(self.search_method).arity
58
- args = [@query,@request,@options]
59
- if search_method_arity < 0
60
- @dbi.klass.send(self.search_method.to_sym,@query,@request,@options)
61
- elsif search_method_arity == 0
62
- raise ArgumentError, "#{@dbi.klass.to_s} method #{search_method} must accept at least 1 argument."
63
- else
64
- arity_limit = search_method_arity > args.size ? args.size : search_method_arity
65
- @dbi.klass.send(self.search_method.to_sym,*(args.slice(0..(arity_limit-1))))
66
- end
67
- end
68
-
69
- def run_default_search
70
- @dbi.search(@query,@options || {})
71
- end
72
- end
73
-
74
- end
75
- end
1
+ module Lolita
2
+ module Search
3
+
4
+ # Default search class for Lolita::Search. Lolita::Configuration::Search uses this by default.
5
+ # It accepts method name as constructor argument, when none is given it call Lolita::DBI#search.
6
+ class Simple
7
+
8
+ # Method in model used to run a search.
9
+ attr_accessor :search_method
10
+ attr_reader :dbi
11
+
12
+ # Accepts search method as optional argument
13
+ def initialize(dbi, *args)
14
+ @dbi = dbi
15
+ @options = args.extract_options!
16
+ @search_method = args[0]
17
+ end
18
+
19
+ # Require dbi (Lolita::DBI instance), query (String) and request and dbi as optional argument.
20
+ # Also you can pass options.
21
+ # ====Example
22
+ # search.run("query",:fields => [:name])
23
+ # # this will search only in :name field
24
+ # search.run("query",nil, Lolita::DBI::Base.create(Category))
25
+ # # this will use Category dbi for search
26
+ # When there is search method defined, it uses that otherwise run default search.
27
+ def run(query,*args)
28
+ with_query(query,*args) do
29
+ if self.search_method
30
+ run_custom_search
31
+ else
32
+ run_default_search
33
+ end
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def with_query(query,*args)
40
+ begin
41
+ options = args.extract_options!
42
+ @old_dbi = self.dbi
43
+ @old_options = @options
44
+ @options = options if options.any?
45
+ @dbi = args[1] if args[1]
46
+ @query = query
47
+ @request = args[0]
48
+ yield
49
+ ensure
50
+ @dbi = @old_dbi
51
+ @options = @old_options
52
+ @query,@request = nil,nil
53
+ end
54
+ end
55
+
56
+ def run_custom_search
57
+ search_method_arity = @dbi.klass.method(self.search_method).arity
58
+ args = [@query,@request,@options]
59
+ if search_method_arity < 0
60
+ @dbi.klass.send(self.search_method.to_sym,@query,@request,@options)
61
+ elsif search_method_arity == 0
62
+ raise ArgumentError, "#{@dbi.klass.to_s} method #{search_method} must accept at least 1 argument."
63
+ else
64
+ arity_limit = search_method_arity > args.size ? args.size : search_method_arity
65
+ @dbi.klass.send(self.search_method.to_sym,*(args.slice(0..(arity_limit-1))))
66
+ end
67
+ end
68
+
69
+ def run_default_search
70
+ @dbi.search(@query,@options || {})
71
+ end
72
+ end
73
+
74
+ end
75
+ end