decidim-core 0.12.0.pre → 0.12.0

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.
@@ -18,11 +18,16 @@ module Decidim
18
18
 
19
19
  included do
20
20
  has_many :searchable_resources, class_name: "Decidim::SearchableResource", inverse_of: :resource, foreign_key: :resource_id, dependent: :destroy
21
- after_create :add_to_index_as_search_resource
22
- after_update :update_index_for_search_resource
21
+ # after_create and after_update callbacks are dynamically setted in `searchable_fields` method.
23
22
 
24
- # Public: after_create callback to index the model as a SearchableResource.
23
+ # Public: after_create callback to index the model as a SearchableResource, if configured so.
25
24
  #
25
+ def try_add_to_index_as_search_resource
26
+ return unless self.class.search_resource_fields_mapper.index_on_create?(self)
27
+ add_to_index_as_search_resource
28
+ end
29
+
30
+ # Forces the model to be indexed for the first time.
26
31
  def add_to_index_as_search_resource
27
32
  fields = self.class.search_resource_fields_mapper.mapped(self)
28
33
  fields[:i18n].keys.each do |locale|
@@ -32,10 +37,18 @@ module Decidim
32
37
 
33
38
  # Public: after_update callback to update index information of the model.
34
39
  #
35
- def update_index_for_search_resource
36
- fields = self.class.search_resource_fields_mapper.mapped(self)
37
- searchable_resources.each do |sr|
38
- sr.update(contents_to_searchable_resource_attributes(fields, sr.locale))
40
+ def try_update_index_for_search_resource
41
+ if self.class.search_resource_fields_mapper.index_on_update?(self)
42
+ if searchable_resources.empty?
43
+ add_to_index_as_search_resource
44
+ else
45
+ fields = self.class.search_resource_fields_mapper.mapped(self)
46
+ searchable_resources.each do |sr|
47
+ sr.update(contents_to_searchable_resource_attributes(fields, sr.locale))
48
+ end
49
+ end
50
+ elsif searchable_resources.any?
51
+ searchable_resources.clear
39
52
  end
40
53
  end
41
54
 
@@ -66,8 +79,8 @@ module Decidim
66
79
  @search_resource_indexable_fields
67
80
  end
68
81
 
69
- # Declares the searchable fields for this instance.
70
- # Must be a Hash that follow the following format:
82
+ # Declares the searchable fields for this instance and, optionally, some conditions.
83
+ # `declared_fields` must be a Hash that follow the following format:
71
84
  # {
72
85
  # scope_id: { scope: :id },
73
86
  # participatory_space: { feature: :participatory_space },
@@ -76,9 +89,23 @@ module Decidim
76
89
  # C: :somehow_relevant_field,
77
90
  # D: [:description, :address]
78
91
  # }
79
- def searchable_fields(declared_fields)
92
+ #
93
+ # `conditions` must be a Hash that only accepts a boolean or a Proc that will be evaluated on runtime and returns a boolean for the following keys:
94
+ # - index_on_create: Whether to index, or not, the current searchabe when it is created. Defaults to true.
95
+ # - index_on_update: Whether to index, or not, the current searchabe when it is updated. Defaults to true.
96
+ #
97
+ def searchable_fields(declared_fields, conditions = {})
80
98
  @search_resource_indexable_fields = SearchResourceFieldsMapper.new(declared_fields)
81
99
  Decidim::Searchable.searchable_resources[name] = self unless Decidim::Searchable.searchable_resources.has_key?(name)
100
+ conditions = { index_on_create: true, index_on_update: true }.merge(conditions)
101
+ if conditions[:index_on_create]
102
+ after_create :try_add_to_index_as_search_resource
103
+ @search_resource_indexable_fields.set_index_condition(:create, conditions[:index_on_create])
104
+ end
105
+ if conditions[:index_on_update]
106
+ after_update :try_update_index_for_search_resource
107
+ @search_resource_indexable_fields.set_index_condition(:update, conditions[:index_on_update])
108
+ end
82
109
  end
83
110
  end
84
111
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decidim-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0.pre
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josep Jaume Rey Peroy
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-06-06 00:00:00.000000000 Z
13
+ date: 2018-06-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: active_link_to
@@ -242,6 +242,20 @@ dependencies:
242
242
  - - "~>"
243
243
  - !ruby/object:Gem::Version
244
244
  version: '4.3'
245
+ - !ruby/object:Gem::Dependency
246
+ name: kaminari
247
+ requirement: !ruby/object:Gem::Requirement
248
+ requirements:
249
+ - - "~>"
250
+ - !ruby/object:Gem::Version
251
+ version: '1.0'
252
+ type: :runtime
253
+ prerelease: false
254
+ version_requirements: !ruby/object:Gem::Requirement
255
+ requirements:
256
+ - - "~>"
257
+ - !ruby/object:Gem::Version
258
+ version: '1.0'
245
259
  - !ruby/object:Gem::Dependency
246
260
  name: loofah
247
261
  requirement: !ruby/object:Gem::Requirement
@@ -594,28 +608,28 @@ dependencies:
594
608
  requirements:
595
609
  - - '='
596
610
  - !ruby/object:Gem::Version
597
- version: 0.12.0.pre
611
+ version: 0.12.0
598
612
  type: :runtime
599
613
  prerelease: false
600
614
  version_requirements: !ruby/object:Gem::Requirement
601
615
  requirements:
602
616
  - - '='
603
617
  - !ruby/object:Gem::Version
604
- version: 0.12.0.pre
618
+ version: 0.12.0
605
619
  - !ruby/object:Gem::Dependency
606
620
  name: decidim-dev
607
621
  requirement: !ruby/object:Gem::Requirement
608
622
  requirements:
609
623
  - - '='
610
624
  - !ruby/object:Gem::Version
611
- version: 0.12.0.pre
625
+ version: 0.12.0
612
626
  type: :development
613
627
  prerelease: false
614
628
  version_requirements: !ruby/object:Gem::Requirement
615
629
  requirements:
616
630
  - - '='
617
631
  - !ruby/object:Gem::Version
618
- version: 0.12.0.pre
632
+ version: 0.12.0
619
633
  description: Adds core features so other engines can hook into the framework.
620
634
  email:
621
635
  - josepjaume@gmail.com
@@ -1186,6 +1200,7 @@ files:
1186
1200
  - config/initializers/rack_attack.rb
1187
1201
  - config/locales/ca.yml
1188
1202
  - config/locales/en.yml
1203
+ - config/locales/es-PY.yml
1189
1204
  - config/locales/es.yml
1190
1205
  - config/locales/eu.yml
1191
1206
  - config/locales/fi.yml
@@ -1298,6 +1313,7 @@ files:
1298
1313
  - db/migrate/20180323102631_change_event_name_and_class_to_rename_to_publish_proposal_event.rb
1299
1314
  - db/migrate/20180508111640_add_tos_version_to_organization.rb
1300
1315
  - db/migrate/20180508111710_add_accepted_tos_version_field_to_users.rb
1316
+ - db/migrate/20180613080638_rename_missing_features_to_components.rb
1301
1317
  - db/seeds.rb
1302
1318
  - db/seeds/homepage_image.jpg
1303
1319
  - lib/decidim/api/attachable_interface.rb
@@ -1465,9 +1481,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
1465
1481
  version: '0'
1466
1482
  required_rubygems_version: !ruby/object:Gem::Requirement
1467
1483
  requirements:
1468
- - - ">"
1484
+ - - ">="
1469
1485
  - !ruby/object:Gem::Version
1470
- version: 1.3.1
1486
+ version: '0'
1471
1487
  requirements: []
1472
1488
  rubyforge_project:
1473
1489
  rubygems_version: 2.7.6