carload 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ec9e29e90114250f401edc897cd90caae9c0d1c0
4
- data.tar.gz: fe00e32e35074658bbfaca1724e9ecb282226ec0
3
+ metadata.gz: a049222076a6d7c860613fca9fdcb808f9d3a1fd
4
+ data.tar.gz: 027d10be270a76dce7187702c936139d6bec61ea
5
5
  SHA512:
6
- metadata.gz: a766a69e3c4f93b543861ada5ddd505a66476e8a231174b443affa73140e9f1dda0515cf415f06b6445d79ac9c14f47f2cf0843f71626f80010aaf10bdecb9a4
7
- data.tar.gz: c6294ff23b3be3049fce81ff4c405b73a75beab1ad545de23e9684c6ac40ccad491009914894ad2c592c1f269804aaf98a7e1962e07f49504b755ef1c5db24de
6
+ metadata.gz: eceb767f554ce9e7cc34b543764485beb0dcc925a70aae2eea3ebdf2d17688e3518a1f2c0083d3dbdc827b370d113314ce47bb78e7a10846cafeadcd6b6615b4
7
+ data.tar.gz: 2421d62bdd80071fb5bd7f41e13829b125d286f4c4315c54f8db465293e82cb614b67e24647cca9d582dfb27277ab86a83edc282474c7dd378972e0b8ebef63b
@@ -17,7 +17,12 @@ module Carload
17
17
  authorize :carload_dashboard, :index? unless Carload.auth_solution == :none
18
18
  if params[:search].present?
19
19
  @query = params[:search][:query]
20
- @objects = @model_class.search(params[:search][:query]).page(params[:page])
20
+ case Carload.search_engine
21
+ when :elasticsearch
22
+ @objects = @model_class.search(params[:search][:query]).records.page(params[:page])
23
+ when :pg_search
24
+ @objects = @model_class.search(params[:search][:query]).page(params[:page])
25
+ end
21
26
  else
22
27
  @objects = @model_class.page(params[:page])
23
28
  end
@@ -12,7 +12,7 @@
12
12
  <div id='main-container'>
13
13
  <div id='top-nav-buttons' class='center'>
14
14
  <div class='btn-group' role='group'>
15
- <%= link_to t('carload.action.back_to_root'), main_app.root_path, class: 'btn btn-default' %>
15
+ <%= link_to t('carload.action.back_to_root'), (main_app.root_path rescue root_path), class: 'btn btn-default' %>
16
16
  <% Dashboard.models.keys.each do |model| %>
17
17
  <%= link_to t("activerecord.models.#{model}"), dashboard_index_path(model),
18
18
  class: "#{@model_name == model ? 'active' : ''} btn btn-default" %>
@@ -18,41 +18,57 @@ module Carload
18
18
  end
19
19
  end
20
20
  # Reopen model classes to add pg text search.
21
- Dictionaries = {
22
- en: 'english',
23
- :'zh-CN' => 'zhparser'
24
- }.freeze
25
- Dashboard.models.each do |name, spec|
26
- # NOTE: Only direct columns are included.
27
- attributes = spec.index_page.searches.attributes.select { |x| x[:name].class == Symbol }.map { |x| x[:name] }
28
- spec.klass.class_eval do
29
- include PgSearch
30
- pg_search_scope :search,
31
- against: attributes,
32
- using: {
33
- tsearch: {
34
- prefix: true,
35
- negation: true,
36
- dictionary: Dictionaries[I18n.locale]
37
- }
38
- }
39
- end
40
- end
41
- # Reopen model classes to handle polymorphic association.
42
- Dashboard.models.each do |name, spec|
43
- spec.associated_models.values.select { |x| x[:polymorphic] == true }.each do |associated_model|
44
- polymorphic_objects = []
45
- associated_model[:instance_models].each do |instance_model|
46
- Dashboard.model(instance_model).klass.all.each do |object|
47
- polymorphic_objects << ["#{object.class} - #{object.send(associated_model[:choose_by])}", "#{object.id},#{object.class}"]
48
- end
21
+ case Carload.search_engine
22
+ when :elasticsearch
23
+ Dashboard.models.each do |name, spec|
24
+ spec.klass.class_eval do
25
+ include Elasticsearch::Model
26
+ include Elasticsearch::Model::Callbacks
27
+
28
+ self.__elasticsearch__.client.indices.delete index: self.index_name rescue nil
29
+ self.__elasticsearch__.client.indices.create \
30
+ index: self.index_name,
31
+ body: { settings: self.settings.to_hash, mappings: self.mappings.to_hash }
49
32
  end
33
+ spec.klass.import
34
+ end
35
+ when :pg_search
36
+ Dictionaries = {
37
+ en: 'english',
38
+ :'zh-CN' => 'zhparser'
39
+ }.freeze
40
+ Dashboard.models.each do |name, spec|
41
+ # NOTE: Only direct columns are included.
42
+ attributes = spec.index_page.searches.attributes.select { |x| x[:name].class == Symbol }.map { |x| x[:name] }
50
43
  spec.klass.class_eval do
51
- class_eval <<-RUBY
52
- def self.#{associated_model[:name].to_s.pluralize}
53
- #{polymorphic_objects}
44
+ include PgSearch
45
+ pg_search_scope :search,
46
+ against: attributes,
47
+ using: {
48
+ tsearch: {
49
+ prefix: true,
50
+ negation: true,
51
+ dictionary: Dictionaries[I18n.locale]
52
+ }
53
+ }
54
+ end
55
+ end
56
+ # Reopen model classes to handle polymorphic association.
57
+ Dashboard.models.each do |name, spec|
58
+ spec.associated_models.values.select { |x| x[:polymorphic] == true }.each do |associated_model|
59
+ polymorphic_objects = []
60
+ associated_model[:instance_models].each do |instance_model|
61
+ Dashboard.model(instance_model).klass.all.each do |object|
62
+ polymorphic_objects << ["#{object.class} - #{object.send(associated_model[:choose_by])}", "#{object.id},#{object.class}"]
54
63
  end
55
- RUBY
64
+ end
65
+ spec.klass.class_eval do
66
+ class_eval <<-RUBY
67
+ def self.#{associated_model[:name].to_s.pluralize}
68
+ #{polymorphic_objects}
69
+ end
70
+ RUBY
71
+ end
56
72
  end
57
73
  end
58
74
  end
@@ -14,4 +14,10 @@ module Carload
14
14
  @message = "Carload does not support #{data} currently."
15
15
  end
16
16
  end
17
+
18
+ class InvalidError < Error
19
+ def initialize data
20
+ @message = data
21
+ end
22
+ end
17
23
  end
@@ -1,3 +1,3 @@
1
1
  module Carload
2
- VERSION = '0.4.1'
2
+ VERSION = '0.4.2'
3
3
  end
data/lib/carload.rb CHANGED
@@ -1,6 +1,11 @@
1
1
  # Require all dependencies (Rails does not do this for us in default).
2
2
  Gem.loaded_specs['carload'].dependencies.each do |dependency|
3
- require dependency.name
3
+ begin
4
+ require dependency.name
5
+ rescue LoadError
6
+ # For elasticsearch-model and elasticsearch-rails
7
+ require dependency.name.gsub('-', '/')
8
+ end
4
9
  end
5
10
 
6
11
  require 'carload/extended_hash'
@@ -24,6 +29,10 @@ module Carload
24
29
  if not [:carrierwave].include? @@config[:upload_solution]
25
30
  raise UnsupportedError.new("upload solution #{@@config[:upload_solution]}")
26
31
  end
32
+ @@config[:search_engine] ||= :elasticsearch
33
+ if not [:elasticsearch, :pg_search].include? @@config[:search_engine]
34
+ raise UnsupportedError.new("search engine #{@@config[:search_engine]}")
35
+ end
27
36
  # Define configuation helpers.
28
37
  @@config.each do |key, value|
29
38
  define_singleton_method key do
@@ -29,10 +29,16 @@ require 'carload'
29
29
 
30
30
  def copy_migration_files
31
31
  # Copy migrations if necessary.
32
- case I18n.locale
33
- when :'zh-CN'
34
- ['20161030074822_carload_enable_zhparser_extension.rb'].each do |file|
35
- copy_file "#{Carload::Engine.root}/db/migrate/#{file}", "db/migrate/#{file}"
32
+ if Carload.search_engine == :pg_search
33
+ adapter = ActiveRecord::Base.connection.instance_values["config"][:adapter]
34
+ if adapter != 'postgresql'
35
+ raise InvalidError.new("Database adapter #{adapter} cannot work with pg_search!")
36
+ end
37
+ case I18n.locale
38
+ when :'zh-CN'
39
+ ['20161030074822_carload_enable_zhparser_extension.rb'].each do |file|
40
+ copy_file "#{Carload::Engine.root}/db/migrate/#{file}", "db/migrate/#{file}"
41
+ end
36
42
  end
37
43
  end
38
44
  end
@@ -17,6 +17,9 @@ Carload.setup do |config|
17
17
  # Set which file upload solution is used. Currently, we only support Carrierwave.
18
18
  config.upload_solution = :carrierwave
19
19
 
20
+ # Set which search engine to use (elasticsearch or pg_search).
21
+ config.search_engine = :elasticsearch
22
+
20
23
  # Set the actions used to discern user's permission to access dashboard.
21
24
  #
22
25
  # config.dashboard.permits_user.<method> = '...'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Li Dong
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-06 00:00:00.000000000 Z
11
+ date: 2016-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -142,6 +142,34 @@ dependencies:
142
142
  - - "~>"
143
143
  - !ruby/object:Gem::Version
144
144
  version: '1.0'
145
+ - !ruby/object:Gem::Dependency
146
+ name: elasticsearch-model
147
+ requirement: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - "~>"
150
+ - !ruby/object:Gem::Version
151
+ version: '0.1'
152
+ type: :runtime
153
+ prerelease: false
154
+ version_requirements: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - "~>"
157
+ - !ruby/object:Gem::Version
158
+ version: '0.1'
159
+ - !ruby/object:Gem::Dependency
160
+ name: elasticsearch-rails
161
+ requirement: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - "~>"
164
+ - !ruby/object:Gem::Version
165
+ version: '0.1'
166
+ type: :runtime
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - "~>"
171
+ - !ruby/object:Gem::Version
172
+ version: '0.1'
145
173
  - !ruby/object:Gem::Dependency
146
174
  name: kaminari
147
175
  requirement: !ruby/object:Gem::Requirement