graph_starter 0.4.0 → 0.4.1

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: 4537a9fa443ead4fb371c58a6353c7b11b61b776
4
- data.tar.gz: bba04ede1062f46677f6a5feeb91dc601e2c0f1f
3
+ metadata.gz: 8c62eaa30a0e1b703a7565ba225b97e7b4639006
4
+ data.tar.gz: 350c64371b1f49d43e3ad5a5a3d8835b26a49c64
5
5
  SHA512:
6
- metadata.gz: 166ca5a40abc2b1664ce121f50c930d047976440f8810831bb399d615e9e29ec24b2ea60e1ee475bb21041c179f1d01bf69203ebbea7167d5e91356a96db64b2
7
- data.tar.gz: d2576210af4ee32f4c9714d6195b0fd0583f349a1add17e5a809319c1877cd6f6e530a8e27bac405f58d7c9af3321aad95df95e5b5545ec277bd051050323b74
6
+ metadata.gz: fea3be17b5a3518836b448fe14ebbcd129cb77427f645a821c83397d2f316b121c76cc490d1b2b0ed6afe31c8834ae1980d5a171a0449b5391bb8fd24d67fb36
7
+ data.tar.gz: 321801dd33ef789366f29cd680e1b841949404f302b107d07b3bd7bec4d1a42aa2552eb358c465667893f41874e2c9289563681dbbb9dda11620ebc423e46624
@@ -6,6 +6,6 @@ module GraphStarter
6
6
  def model_class
7
7
  @model_slug = params[:model_slug]
8
8
  @model_slug.classify.constantize
9
- end
9
+ end
10
10
  end
11
11
  end
@@ -8,31 +8,41 @@ module GraphStarter
8
8
  end
9
9
 
10
10
  def search
11
- regexp = Regexp.new('.*' + params[:query].gsub(/\s+/, '.*') + '.*', 'i')
12
- assets = asset_set { |scope| scope.where(title: regexp) }
11
+ assets = asset_set { |scope| scope.for_query(params[:query]) }
13
12
  results_data = assets.map do |asset|
13
+ description = model_class.search_properties.map do |property|
14
+ "<b>#{property.to_s.humanize}:</b> #{asset.read_attribute(property)}"
15
+ end.join("<br>")
16
+
14
17
  {
15
18
  title: asset.title,
16
19
  url: asset_path(id: asset, model_slug: asset.class.model_slug),
17
- image: images? && asset.first_image_source && asset.first_image_source.url || nil
18
- }.reject {|_, v| v.nil? }
20
+ description: description,
21
+ image: images? && asset.first_image_source_url || nil
22
+ }.reject {|_, v| v.nil? }.tap do |result|
23
+ model_class.search_properties.each do |property|
24
+ result[property] = asset.read_attribute(property)
25
+ end
26
+ end
19
27
  end
20
28
 
29
+ # INCLUDE SEARCH QUERY PROPERTIES IN RESULT!!!
30
+
21
31
  render json: {results: results_data}.to_json
22
32
  end
23
33
 
24
- def asset_set
34
+ def asset_set(var = :asset)
25
35
  associations = []
26
36
  associations << :images if images?
27
37
  associations << model_class.category_association if model_class.category_association
28
38
 
29
- scope = model_class_scope
39
+ scope = model_class_scope(var)
30
40
  scope = yield scope if block_given?
31
41
 
32
42
  scope = scope.limit(50)
33
43
 
34
44
  if associations.present?
35
- scope.query_as(:s).with(:s).proxy_as(model_class_scope.model, :s).with_associations(*associations)
45
+ scope.query_as(var).with(var).proxy_as(model_class, var).with_associations(*associations)
36
46
  else
37
47
  scope
38
48
  end
@@ -78,12 +88,14 @@ module GraphStarter
78
88
  model_class_scope.has_images?
79
89
  end
80
90
 
81
- def model_class_scope
82
- @model_class_scope = if defined?(current_user)
83
- model_class.authorized_for(current_user)
84
- else
85
- model_class.all
86
- end
91
+ def model_class_scope(var = :asset)
92
+ #@model_class_scope = if defined?(current_user)
93
+ # model_class.authorized_for(current_user)
94
+ #else
95
+ # model_class.all(var)
96
+ #end
97
+
98
+ @model_class_scope ||= model_class.all(var)
87
99
  end
88
100
  end
89
101
  end
@@ -117,8 +117,22 @@ module GraphStarter
117
117
  end
118
118
 
119
119
 
120
+ def self.search_properties(*array)
121
+ if array.empty?
122
+ @search_properties || [name_property]
123
+ else
124
+ @search_properties = array
125
+ end
126
+ end
127
+
120
128
  def self.for_query(query)
121
- all.where(title: /.*#{query}.*/i)
129
+ where_clause = self.search_properties.map do |property|
130
+ fail "Invalid property: #{property}" if attributes[property].nil?
131
+ "asset.#{property} =~ {query}"
132
+ end.join(' OR ')
133
+
134
+ query_string = query.strip.gsub(/\s+/, '.*')
135
+ all(:asset).where(where_clause).params(query: "(?i).*#{query_string}.*")
122
136
  end
123
137
 
124
138
  def secret_sauce_recommendations
@@ -9,7 +9,7 @@
9
9
  .ui.divider
10
10
 
11
11
  javascript:
12
- var controller = #{@model_class_scope.model_slug.to_json.html_safe}
12
+ var controller = #{@model_slug.to_json.html_safe}
13
13
 
14
14
  coffee:
15
15
  $('.ui.search').search
@@ -25,4 +25,4 @@
25
25
 
26
26
  .ui.divider
27
27
 
28
- = render partial: 'graph_starter/assets/cards', locals: {assets: @assets}
28
+ = render partial: 'graph_starter/assets/cards', locals: {assets: @assets}
@@ -1,3 +1,3 @@
1
1
  module GraphStarter
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graph_starter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Underwood