tablets 0.1.0 → 0.2.0

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: 1a1fa5246fe01b026cb2cb2eea4d8a1bdf1bb864
4
- data.tar.gz: d41e7ccd372d8f2a63b702c77521a874e4baf4bc
3
+ metadata.gz: 073682f3e75df008250ab8ec39352ed307eb797e
4
+ data.tar.gz: e811d153b45b6bce4396f9711aa45e2c039b6fea
5
5
  SHA512:
6
- metadata.gz: ab12d327506c9abfc4083855019fadffcddd399db62098b44f788a5abc91e823969df57e140214f9c5d38fb6047de374dc920281c128ae84c649d7c51235ac42
7
- data.tar.gz: 8a0c9608f4dfb7deb525674287114fef5f6090bca34ec10daa1a0e8b8df4342c0cb7ae6b99382ff5053323774d63d2fa64b4c30cdfa16357b14a45a22bcb9563
6
+ metadata.gz: c0603710b65211af597d17c3015e6b175871f44c3d5d417f9560fe810c0c6c172e1bfe90d4472beb85e31d81d2e67902274a151a34210bbbd16a6362a429f511
7
+ data.tar.gz: 086fe45cd22de329a6fc4da74dcc5f273cccdd4e724380d15ff3e4c24e3d8457b0e59c848288f4dd7db1d38747fd31e99f0f38b445d0f8aafb214ff2fd2e0d50
data/README.md CHANGED
@@ -1,33 +1,39 @@
1
1
  # Tablets
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/tablets.svg)](http://badge.fury.io/rb/tablets)
4
+
3
5
  ## Installation
4
6
 
5
7
  Add this line to your application's `Gemfile`:
6
8
 
7
- ```ruby
9
+ ``` ruby
8
10
  gem 'tablets'
9
11
  ```
10
12
 
11
13
  Include tablets and jquery datatables scripts in your `app/assets/application.js`:
12
- ```
14
+
15
+ ``` javascript
13
16
  //= require dataTables/jquery.dataTables
14
17
  //= require tablets
15
18
  ```
16
19
 
17
20
  Include jquery datatables styles in your `app/assets/application.css`:
21
+
18
22
  ```
19
23
  *= require dataTables/jquery.dataTables
20
24
  ```
21
25
 
22
26
  Mount engine in `config/routes.rb`:
23
- ```
27
+
28
+ ``` ruby
24
29
  mount Tablets::Engine, at: '/tablets'
25
30
  ```
26
31
 
27
32
  ## Examples
28
33
 
29
34
  In `app/tablets/posts.rb`:
30
- ```ruby
35
+
36
+ ``` ruby
31
37
  Tablets.register :posts do
32
38
  # Authorization logic.
33
39
  authorize do |controller|
@@ -43,13 +49,14 @@ Tablets.register :posts do
43
49
  columns do
44
50
  [{
45
51
  title: 'Title',
46
- order: 'posts.title',
47
52
  data: :title,
53
+ order: 'posts.title',
54
+ search: 'posts.title',
48
55
  options: { className: 'post-title' }
49
56
  }, {
50
57
  title: 'User',
51
- order: ['users.first_name', 'users.last_name'],
52
58
  data: ->(user) { [user.first_name, user.last_name].compact.join(' ') },
59
+ order: ['users.first_name', 'users.last_name'],
53
60
  search: ['users.first_name', 'users.last_name']
54
61
  }]
55
62
  end
@@ -72,19 +79,41 @@ Tablets.register :posts do
72
79
  end
73
80
  ```
74
81
 
75
- In `app/views/posts/index.html.erb`
76
- ```erb
82
+ In `app/views/posts/index.html.erb`:
83
+
84
+ ``` erb
77
85
  <%= render_tablet :posts, user_id: current_user.id %>
78
86
  ```
79
87
 
80
- ## JavaScripts callbacks
88
+ In `app/assets/javascripts/posts.js`:
89
+
90
+ ``` javascript
91
+ $('.tablet[name="posts"]').on('click', 'tbody > tr[role=row]', function() {
92
+ Tablets.toggleDetails($(this).closest('tr'));
93
+ });
94
+
95
+ ```
96
+
97
+ Use `'.tablet.has-details'` for all tablets that has details.
98
+
99
+ ## JavaScript callbacks
81
100
 
82
101
  Called just before datatable initialization.
83
- ```javascript
84
- Tablets.Callbacks.beforeInit = function(element, options) {}
102
+
103
+ ``` javascript
104
+ Tablets.Callbacks.beforeInit = function(element, options) {};
85
105
  ```
86
106
 
87
107
  Called after datatable initialization.
88
- ```javascript
89
- Tablets.Callbacks.afterInit = function(table) {}
108
+
109
+ ``` javascript
110
+ Tablets.Callbacks.afterInit = function(table) {};
111
+ ```
112
+
113
+ ## JavaScript API
114
+
115
+ Open details for row.
116
+
117
+ ``` javascript
118
+ Tablets.toggleDetails($tr);
90
119
  ```
data/Rakefile CHANGED
@@ -11,6 +11,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
11
11
  rdoc.title = 'Tablets'
12
12
  rdoc.options << '--line-numbers'
13
13
  rdoc.rdoc_files.include('lib/**/*.rb')
14
+ rdoc.rdoc_files.include('README.md')
14
15
  end
15
16
 
16
17
  Bundler::GemHelper.install_tasks
@@ -0,0 +1,3 @@
1
+ $.extend @Tablets,
2
+ toggleDetails: ($tr) ->
3
+ @tabletForElement($tr).toggleDetails($tr)
@@ -0,0 +1,5 @@
1
+ $.extend @Tablets,
2
+ tabletId: ($element) ->
3
+ $element.closest('.tablet').attr('id')
4
+ tabletForElement: ($element) ->
5
+ Tablets.get(@tabletId($element))
@@ -0,0 +1,5 @@
1
+ $.extend @Tablets,
2
+ set: (id, tablet) ->
3
+ (@store ||= {})[id] = tablet
4
+ get: (id) ->
5
+ (@store ||= {})[id]
@@ -1,7 +1,8 @@
1
1
  class Tablets.Tablet
2
2
  constructor: (@element, @options, @params) ->
3
3
  @initTable()
4
- @initDetails()
4
+
5
+ Tablets.set(@element.attr('id'), this)
5
6
 
6
7
  dataTableOptions: ->
7
8
  $.extend {}, @options,
@@ -17,19 +18,15 @@ class Tablets.Tablet
17
18
 
18
19
  Tablets.Callbacks.afterInit(@table)
19
20
 
20
- initDetails: ->
21
- table = @table
22
- table.on 'click', 'tbody > tr[role=row] > td:first-child', ->
23
- tr = $(this).closest 'tr'
24
- row = table.row tr
25
-
26
- if row.child.isShown()
27
- row.child.hide()
28
- tr.removeClass 'expanded'
29
- tr.removeClass 'shown'
30
- else
31
- data = row.data().details
32
- if data
33
- row.child("<span>#{data}</span>").show()
34
- tr.addClass 'expanded'
35
- tr.addClass 'shown'
21
+ toggleDetails: (tr) ->
22
+ row = @table.row tr
23
+
24
+ if row.child.isShown()
25
+ row.child.hide()
26
+ tr.removeClass 'expanded'
27
+ tr.removeClass 'shown'
28
+ else
29
+ data = row.data().details
30
+ row.child("<span>#{data}</span>").show()
31
+ tr.addClass 'expanded'
32
+ tr.addClass 'shown'
@@ -1,4 +1,4 @@
1
- <table class="table tablet" id="<%= id %>" data-name="<%= name %>">
1
+ <table class="table tablet <%= 'has-details' if has_details %>" id="<%= id %>" data-name="<%= name %>">
2
2
  <thead>
3
3
  <tr>
4
4
  <% columns.each.with_index do |column, index| %>
@@ -10,7 +10,7 @@ module Tablets
10
10
  end
11
11
 
12
12
  # Applies processing on relation. Need to be implemented in descendants.
13
- def apply(relation)
13
+ def apply(_relation)
14
14
  fail NotImplementedError, '#apply need to be overrided by processing.'
15
15
  end
16
16
 
@@ -7,8 +7,8 @@ module Tablets
7
7
  class Order < Tablets::Data::Processing::Base
8
8
  # Applies order processing on relation.
9
9
  def apply(relation)
10
- params[:order].values.inject(relation) do |relation, item|
11
- relation.order("#{column(item)} #{direction(item)}")
10
+ params[:order].values.inject(relation) do |rel, item|
11
+ rel.order("#{column(item)} #{direction(item)}")
12
12
  end
13
13
  end
14
14
 
@@ -25,7 +25,8 @@ module Tablets
25
25
  name: tablet.name,
26
26
  columns: tablet.columns,
27
27
  options: options,
28
- params: params
28
+ params: params,
29
+ has_details: tablet.details?
29
30
  }
30
31
  end
31
32
 
@@ -35,6 +35,11 @@ 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
+
38
43
  # Returns columns definitions.
39
44
  # Required.
40
45
  def columns
@@ -49,6 +54,11 @@ module Tablets
49
54
 
50
55
  private
51
56
 
57
+ # Checks if config has callback with specified name.
58
+ def has?(name)
59
+ @config.has?(name)
60
+ end
61
+
52
62
  # Calls callback.
53
63
  # Clarifies error message on error.
54
64
  def call(callback, *params, &block)
@@ -32,6 +32,11 @@ module Tablets
32
32
  instance_eval(&block)
33
33
  end
34
34
 
35
+ # Checks if value or callback is defined with specified name.
36
+ def has?(name)
37
+ @hash[name].present?
38
+ end
39
+
35
40
  # Returns value.
36
41
  # If no value defined, returns default.
37
42
  # If no default raises ArgumentError.
@@ -1,3 +1,3 @@
1
1
  module Tablets
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -49,7 +49,10 @@ RSpec.describe Tablets::Renderer do
49
49
 
50
50
  before do
51
51
  allow(tablet).to receive_messages(
52
- name: tablet_name, columns: tablet_columns, options: tablet_options
52
+ name: tablet_name,
53
+ columns: tablet_columns,
54
+ options: tablet_options,
55
+ details?: false
53
56
  )
54
57
 
55
58
  allow(Tablets::Engine).to receive_message_chain(
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.1.0
4
+ version: 0.2.0
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-11 00:00:00.000000000 Z
11
+ date: 2015-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -106,6 +106,9 @@ files:
106
106
  - Rakefile
107
107
  - app/assets/javascripts/tablets.js.coffee
108
108
  - app/assets/javascripts/tablets/callbacks.js.coffee
109
+ - app/assets/javascripts/tablets/details.js.coffee
110
+ - app/assets/javascripts/tablets/navigation.js.coffee
111
+ - app/assets/javascripts/tablets/store.js.coffee
109
112
  - app/assets/javascripts/tablets/tablet.js.coffee
110
113
  - app/controllers/tablets/ajax_controller.rb
111
114
  - app/views/tablets/_tablet.html.erb