record_collection 0.5.2 → 0.5.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1221e77bb333dddfa1f9d2cfb8d6dae31aa3ef7f
4
- data.tar.gz: d532989e28726b95921beb9450679af4003a8a10
3
+ metadata.gz: 4abda6b9b06f53f8caacf070513cfe5b3f7fa4be
4
+ data.tar.gz: beec42bfc69c04587a0baa1f722c6e0dd99ea6db
5
5
  SHA512:
6
- metadata.gz: 11d725ea89a210e0486a91a3e5e47b9c1a2aecc1ef539bf95f25ed5cf8d5710436761a4c4a2d18890d808b38ab8eea6787c4c48867dcfed2b7ce046599524580
7
- data.tar.gz: 37602afb6e7d8efa2c5ccbdd06b42367ca44b06929bc4f1e48b5f989ba374e936d805c9af17fea56a6f17980b64b3014e68f9869294c8daab38a143798d33a31
6
+ metadata.gz: 94846e1eb01ff6e90afd9b3f58d9d34bfc21d697b9a8e5f57c15883549201226dd3edb6aa537df4425f5eabbcab74bc2001190cfa01e8339d36c2cf886b5583f
7
+ data.tar.gz: c0b027639f916a9475a769f0c151c48e28d6a30f5ba1080202640866eedcda28f03c12e76a26debd9842e2b8ba6fed4e11763c986047c0a502d83418ab687955
@@ -1,8 +1,19 @@
1
1
  class MultiSelect
2
- setup: (table) ->
2
+ callbacks: {}
3
+ on: (event, callback) ->
4
+ @callbacks[event] ||= []
5
+ @callbacks[event].push callback
6
+ trigger: (event)->
7
+ return unless @callbacks[event]
8
+ for callback in @callbacks[event]
9
+ callback.call(@)
10
+ setup: (table, options) ->
3
11
  return unless table and table.length
12
+ selector = @
4
13
  table.data 'multi_select', @
5
14
  @set 'table', table
15
+ @options = options
16
+ @on 'changed', options.changed if options.changed
6
17
 
7
18
  @set 'resource', table.data('resource')
8
19
  # Add an extra th to all header rows
@@ -20,6 +31,7 @@ class MultiSelect
20
31
  else
21
32
  table.find('td.selection .checker').removeClass('unchecked').addClass('checked')
22
33
  $(this).removeClass('unchecked').addClass('checked')
34
+ selector.trigger 'changed'
23
35
  table.find('thead tr:last th:first').append toggle_all
24
36
 
25
37
  # Create a toggle/checkbox for all records and add it to the row
@@ -29,6 +41,7 @@ class MultiSelect
29
41
  $(this).removeClass('checked').addClass('unchecked')
30
42
  else
31
43
  $(this).removeClass('unchecked').addClass('checked')
44
+ selector.trigger 'changed'
32
45
  record_td = $('<td></td>').addClass('selection').append(record_toggle)
33
46
  table.find('tbody tr').prepend record_td
34
47
 
@@ -41,19 +54,22 @@ class MultiSelect
41
54
  set: (path, value) -> @[path] = value
42
55
  get: (path) -> @[path]
43
56
 
57
+ selected_count: -> @table.find('td.selection .checked').length
58
+ total_count: -> @table.find('td.selection .checker').length
59
+
44
60
  #selected_records: -> @table.find("td.selection input:checked").map( -> $(this).parents('tr').data('record') )
45
61
  selected_records: -> @table.find("td.selection .checked").map( -> $(this).parents('tr').data('record') )
46
- selected_ids: -> @selected_records().map( -> this.id ).toArray()
62
+ selected_ids: -> @selected_records().map( -> @id ).toArray()
47
63
  root = @
48
64
  root.MultiSelect = new MultiSelect()
49
- $.fn.multi_select = ->
65
+ $.fn.multi_select = (options = {})->
50
66
  if @.hasClass('with-selection') or @.prop('tagName') is 'TABLE'
51
67
  select = new MultiSelect()
52
68
  root.MultiSelect = select
53
- select.setup @
69
+ select.setup @, options
54
70
  else
55
71
  @.find('table.with-selection').each (i, el)->
56
72
  select = new MultiSelect()
57
73
  root.MultiSelect = select
58
- select.setup $(el)
74
+ select.setup $(el), options
59
75
  select
@@ -82,6 +82,13 @@ class Optionals
82
82
  #value_toggle.hide()
83
83
  container.removeClass('active').addClass('inactive')
84
84
  activator_label = $('<span></span>').addClass('optional-input-activator-label').text label_text
85
+ activator_label.click ->
86
+ if activator_container.hasClass('active')
87
+ # Focus on element
88
+ value_field.focus()
89
+ else
90
+ # Activate the optional
91
+ activator_toggle.click()
85
92
  activator_container.append activator_toggle
86
93
  activator_container.append activator_label
87
94
 
@@ -125,7 +125,7 @@ module RecordCollection
125
125
  end
126
126
 
127
127
  def ids
128
- @ids ||= map(&:id)
128
+ @ids ||= map{|record| record.try(:id) }.compact
129
129
  end
130
130
  end
131
131
  end
@@ -1,3 +1,3 @@
1
1
  module RecordCollection
2
- VERSION = "0.5.2"
2
+ VERSION = "0.5.3"
3
3
  end
@@ -8,6 +8,11 @@ RSpec.describe RecordCollection::Base do
8
8
  described_class.new([employee]).ids.should eq [employee.id]
9
9
  end
10
10
 
11
+ it 'Filters out nil records' do
12
+ employee = Employee.create name: 'E1', section: 'ABC', admin: true, vegan: false
13
+ described_class.new([employee, nil]).ids.should eq [employee.id]
14
+ end
15
+
11
16
  it 'returns an empty array for an empty collection' do
12
17
  described_class.new.ids.should be_empty
13
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: record_collection
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin ter Kuile