tablets 0.3.0 → 0.3.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: 08132fccd6b5eb432ef7cad745f40235ab6b6bde
4
- data.tar.gz: fe36e379b7c6157a90fd8e5e218fd205838daa30
3
+ metadata.gz: ada28ad84ab6080afca26791721a0cc50a38ddbd
4
+ data.tar.gz: 545ad23ef11c304211f4095ca35956b9a38c3d44
5
5
  SHA512:
6
- metadata.gz: aad3e1ab54407056b782808eb5ee9c11e0ca58822e9dcc0e2d08842e76e6a281e8e53b1af72dc46ee7c819a0bdc12fbb175a19bfbdba0b96750f64828bcefa97
7
- data.tar.gz: 6a3b5dc7ebbc29fa564376c290c796baa42b0cbd9884017c8af269f26137e4730761b24b2413d552decea370a0e53dc45ba11690affc9ce945ae2ab599a4704b
6
+ metadata.gz: fac271b7c4b42ef7f2e6098f5c5506da9e49ed275687480f1b7b692478ef28b2dc497b5b05db43096fb709a7267f4ee0a75cd80a81cd0a50e76d230d3bbcd189
7
+ data.tar.gz: 0096c9645613c4eea418b968a7e6b960ee806d32cad89c61839e8a64a8ea217573da00823a7b02813e7929c85c435b6ba9c251b8b45afdafa6a634405993f07d
data/README.md CHANGED
@@ -29,6 +29,7 @@ Mount engine in `config/routes.rb`:
29
29
  mount Tablets::Engine, at: '/tablets'
30
30
  ```
31
31
 
32
+
32
33
  ## Examples
33
34
 
34
35
  In `app/tablets/posts.rb`:
@@ -95,6 +96,7 @@ $('.tablet[data-name="posts"]').on('click', 'tbody > tr[role=row]', function() {
95
96
 
96
97
  Use `'.tablet.has-details'` for all tablets that has details.
97
98
 
99
+
98
100
  ## JavaScript API
99
101
 
100
102
  Fields:
@@ -110,6 +112,11 @@ Open details for row.
110
112
  Tablets.toggleDetails($tr);
111
113
  ```
112
114
 
115
+ Returns nearest tablet:
116
+
117
+ Tablets.tabletForElement($element);
118
+
119
+
113
120
  ### JavaScript callbacks
114
121
 
115
122
  Callbacks called on tablet. You can use `this` to access tablet from callback.
@@ -1,5 +1,10 @@
1
1
  $.extend Tablets,
2
+ reload_all: ->
3
+ for _id, tablet of @store
4
+ tablet.reload()
2
5
  tabletId: ($element) ->
3
6
  $element.closest('.tablet').attr('id')
4
7
  tabletForElement: ($element) ->
5
8
  Tablets.get(@tabletId($element))
9
+ toggleDetails: ($tr) ->
10
+ @tabletForElement($tr).toggleDetails($tr)
@@ -1,13 +1,16 @@
1
1
  class Tablets.Tablet
2
2
  constructor: (@element, @options, @params) ->
3
+ @initVars()
3
4
  @initTable()
4
5
 
5
- Tablets.set(@element.attr('id'), this)
6
+ Tablets.set(@id, this)
6
7
 
7
8
  dataTableOptions: ->
8
9
  $.extend {}, @options,
9
10
  fnServerParams: (data) =>
10
11
  data.params = @serverParams()
12
+ fnRowCallback: (row, data) =>
13
+ $(row).addClass('has-details') if data.details
11
14
 
12
15
  serverParams: ->
13
16
  params = $.extend({}, @params)
@@ -16,6 +19,10 @@ class Tablets.Tablet
16
19
 
17
20
  params
18
21
 
22
+ initVars: ->
23
+ @id = @element.attr('id')
24
+ @name = @element.attr('data-name')
25
+
19
26
  initTable: ->
20
27
  options = @dataTableOptions()
21
28
 
@@ -25,6 +32,9 @@ class Tablets.Tablet
25
32
 
26
33
  @trigger('afterInit')
27
34
 
35
+ reload: ->
36
+ @element.dataTable().fnDraw(false)
37
+
28
38
  toggleDetails: (tr) ->
29
39
  row = @table.row tr
30
40
 
@@ -1,4 +1,4 @@
1
- <table class="table tablet <%= 'has-details' if has_details %>" id="<%= id %>" data-name="<%= name %>">
1
+ <table class="table tablet" id="<%= id %>" data-name="<%= name %>">
2
2
  <thead>
3
3
  <tr>
4
4
  <% columns.each.with_index do |column, index| %>
data/lib/tablets/data.rb CHANGED
@@ -41,9 +41,14 @@ module Tablets
41
41
  # Also appends details.
42
42
  def data
43
43
  records.map do |record|
44
- tablet.columns.map.with_index do |column, index|
44
+ data = tablet.columns.map.with_index do |column, index|
45
45
  [index, cell(record, column)]
46
- end.to_h.merge(details: tablet.details(record))
46
+ end.to_h
47
+
48
+ details = tablet.details(record)
49
+ data[:details] = details if details.present?
50
+
51
+ data
47
52
  end
48
53
  end
49
54
 
@@ -25,8 +25,7 @@ module Tablets
25
25
  name: tablet.name,
26
26
  columns: tablet.columns,
27
27
  options: options,
28
- params: params,
29
- has_details: tablet.details?
28
+ params: params
30
29
  }
31
30
  end
32
31
 
@@ -36,7 +35,7 @@ module Tablets
36
35
  # renderer.id #=> 'posts_user_id_1'
37
36
  #
38
37
  def id
39
- [tablet.name, params.to_a].flatten.map(&:to_s).join('_')
38
+ [tablet.name, params.to_a, 'tablet'].flatten.map(&:to_s).join('_')
40
39
  end
41
40
 
42
41
  # Calculates resulting options.
@@ -35,30 +35,40 @@ module Tablets
35
35
  call(:details, record) { nil }
36
36
  end
37
37
 
38
- # Checks if details is defined.
39
- def details?
40
- has?(:details)
41
- end
42
-
43
38
  # Returns columns definitions.
44
- # Required.
39
+ # By default deduct columns from relation.
45
40
  def columns
46
- call(:columns)
41
+ call(:columns) { deduct_columns_from_relation }
47
42
  end
48
43
 
49
44
  # Returns database relation to fetch data.
50
- # Required.
51
- def relation(params)
52
- call(:relation, params)
45
+ # By default tries to deduct class from tablet name.
46
+ def relation(params = {})
47
+ if defined? @name.camelize.constantize
48
+ call(:relation, params) { @name.camelize.constantize.where(nil) }
49
+ else
50
+ call(:relation, params)
51
+ end
53
52
  end
54
53
 
55
- private
56
-
57
54
  # Checks if config has callback with specified name.
58
55
  def has?(name)
59
56
  @config.has?(name)
60
57
  end
61
58
 
59
+ private
60
+
61
+ # Returns default columns definititions, deducted from relation.
62
+ def deduct_columns_from_relation
63
+ relation.columns.map(&:name).map do |name|
64
+ {
65
+ title: name.humanize.capitalize,
66
+ data: name.to_sym,
67
+ order: name
68
+ }
69
+ end
70
+ end
71
+
62
72
  # Calls callback.
63
73
  # Clarifies error message on error.
64
74
  def call(callback, *params, &block)
@@ -1,3 +1,3 @@
1
1
  module Tablets
2
- VERSION = '0.3.0'
2
+ VERSION = '0.3.1'
3
3
  end
@@ -76,7 +76,7 @@ RSpec.describe Tablets::Renderer do
76
76
 
77
77
  it 'passes correct id as partial locals' do
78
78
  expect(view_context).to have_received(:render).with(
79
- locals(id: 'posts_user_id_1')
79
+ locals(id: 'posts_user_id_1_tablet')
80
80
  )
81
81
  end
82
82
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tablets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yevhen Shemet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-22 00:00:00.000000000 Z
11
+ date: 2015-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -105,9 +105,8 @@ files:
105
105
  - README.md
106
106
  - Rakefile
107
107
  - app/assets/javascripts/tablets.js.coffee
108
+ - app/assets/javascripts/tablets/api.js.coffee
108
109
  - app/assets/javascripts/tablets/callbacks.js.coffee
109
- - app/assets/javascripts/tablets/details.js.coffee
110
- - app/assets/javascripts/tablets/navigation.js.coffee
111
110
  - app/assets/javascripts/tablets/store.js.coffee
112
111
  - app/assets/javascripts/tablets/tablet.js.coffee
113
112
  - app/controllers/tablets/ajax_controller.rb
@@ -1,3 +0,0 @@
1
- $.extend Tablets,
2
- toggleDetails: ($tr) ->
3
- @tabletForElement($tr).toggleDetails($tr)