tablets 0.3.9 → 0.3.10
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 +4 -4
- data/app/controllers/tablets/ajax_controller.rb +1 -1
- data/lib/tablets/data/processing/base.rb +4 -4
- data/lib/tablets/data/processing/filter.rb +7 -7
- data/lib/tablets/data/processing/order.rb +16 -6
- data/lib/tablets/data/processing/paginate.rb +4 -5
- data/lib/tablets/data/query.rb +8 -8
- data/lib/tablets/data.rb +12 -12
- data/lib/tablets/engine.rb +1 -1
- data/lib/tablets/global/configurator.rb +3 -3
- data/lib/tablets/global/loader.rb +8 -7
- data/lib/tablets/global/store.rb +3 -3
- data/lib/tablets/railtie.rb +3 -3
- data/lib/tablets/renderer.rb +7 -7
- data/lib/tablets/tablet.rb +18 -19
- data/lib/tablets/utils/config.rb +23 -23
- data/lib/tablets/utils/search_condition_builder.rb +10 -10
- data/lib/tablets/version.rb +2 -2
- data/lib/tablets/view_helpers.rb +2 -2
- data/lib/tablets.rb +1 -1
- metadata +11 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b7e0261127b2b6dcd32cbb870ba4480581a6320b
|
4
|
+
data.tar.gz: b9419a63583a1da3be7af865dbf8d9825b3bb76b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 234ef30615feb35d4f8c2320e30ad55354d91a1790a87ac382fa6d7b65c7bf316daa8525a1cc738a75e9d551882fb8eaca7fdd95c5ae04d505ad6fd6102957a5
|
7
|
+
data.tar.gz: 7385d770428d5139cc64121e9cd3f421e20c5060539053751026a3f65dcf46a49befb4648e45ac5ecee9230ef2319f36e02e07ca2aa35532d950f7042ea0862e
|
@@ -1,20 +1,20 @@
|
|
1
1
|
module Tablets
|
2
2
|
class Data
|
3
3
|
module Processing
|
4
|
-
# Base class for relation processings
|
4
|
+
# Base class for relation processings
|
5
5
|
class Base
|
6
|
-
# Initializes processor with relation and data reqired for processing
|
6
|
+
# Initializes processor with relation and data reqired for processing
|
7
7
|
def initialize(params, columns)
|
8
8
|
@params = params
|
9
9
|
@columns = columns
|
10
10
|
end
|
11
11
|
|
12
|
-
# Applies processing on relation. Need to be implemented in descendants
|
12
|
+
# Applies processing on relation. Need to be implemented in descendants
|
13
13
|
def apply(_relation)
|
14
14
|
fail NotImplementedError, '#apply need to be overrided by processing.'
|
15
15
|
end
|
16
16
|
|
17
|
-
# Shorthand for create processing and apply it
|
17
|
+
# Shorthand for create processing and apply it
|
18
18
|
def self.apply(params, columns, relation)
|
19
19
|
new(params, columns).apply(relation)
|
20
20
|
end
|
@@ -7,16 +7,16 @@ require 'tablets/data/processing/base'
|
|
7
7
|
module Tablets
|
8
8
|
class Data
|
9
9
|
module Processing
|
10
|
-
# Incapsulate relation filtering logic
|
10
|
+
# Incapsulate relation filtering logic
|
11
11
|
class Filter < Tablets::Data::Processing::Base
|
12
|
-
# Applies filter processing on relation
|
12
|
+
# Applies filter processing on relation
|
13
13
|
def apply(relation)
|
14
14
|
search(relation)
|
15
15
|
end
|
16
16
|
|
17
17
|
private
|
18
18
|
|
19
|
-
# Applies search conditions if need
|
19
|
+
# Applies search conditions if need
|
20
20
|
def search(relation)
|
21
21
|
return relation unless params[:search].fetch(:value, nil).present?
|
22
22
|
|
@@ -25,19 +25,19 @@ module Tablets
|
|
25
25
|
relation
|
26
26
|
end
|
27
27
|
|
28
|
-
# Builds search conditions
|
28
|
+
# Builds search conditions
|
29
29
|
def build_conditions_for(query)
|
30
30
|
query.split(' ').map do |value|
|
31
31
|
'(' + search_conditions(value) + ')'
|
32
32
|
end.join(' AND ')
|
33
33
|
end
|
34
34
|
|
35
|
-
# Returns searchable columns
|
35
|
+
# Returns searchable columns
|
36
36
|
def searchable_columns
|
37
|
-
columns.map { |column| column[:search] }.flatten.compact
|
37
|
+
columns.map { |column| column[:search] }.flatten.map(&:to_s).compact
|
38
38
|
end
|
39
39
|
|
40
|
-
# Returs search conditions for each searchable column
|
40
|
+
# Returs search conditions for each searchable column
|
41
41
|
def search_conditions(value)
|
42
42
|
searchable_columns.map do |column|
|
43
43
|
Tablets::Utils::SearchConditionBuilder.new(column, value).build
|
@@ -3,24 +3,25 @@ require 'tablets/data/processing/base'
|
|
3
3
|
module Tablets
|
4
4
|
class Data
|
5
5
|
module Processing
|
6
|
-
# Incapsulate relation ordering logic
|
6
|
+
# Incapsulate relation ordering logic
|
7
7
|
class Order < Tablets::Data::Processing::Base
|
8
|
-
# Applies order processing on relation
|
8
|
+
# Applies order processing on relation
|
9
9
|
def apply(relation)
|
10
10
|
params[:order].values.inject(relation) do |rel, item|
|
11
|
-
|
11
|
+
sorting_direction = direction(item)
|
12
|
+
nulls = null_place(sorting_direction)
|
13
|
+
rel.order("#{column(item)} #{sorting_direction} #{nulls}")
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
15
17
|
private
|
16
18
|
|
17
|
-
# Determines order column from params
|
19
|
+
# Determines order column from params
|
18
20
|
def column(item)
|
19
21
|
columns[item[:column].to_i][:order]
|
20
22
|
end
|
21
23
|
|
22
|
-
# Determines order direction from params
|
23
|
-
# ASC by default.
|
24
|
+
# Determines order direction from params (ASC by default)
|
24
25
|
def direction(item)
|
25
26
|
if %w(ASC DESC).include?(item.fetch(:dir, '').upcase)
|
26
27
|
item[:dir].upcase
|
@@ -28,6 +29,15 @@ module Tablets
|
|
28
29
|
'ASC'
|
29
30
|
end
|
30
31
|
end
|
32
|
+
|
33
|
+
# Determines where nulls appear
|
34
|
+
def null_place(sort)
|
35
|
+
if sort.upcase == 'ASC'
|
36
|
+
'NULLS FIRST'
|
37
|
+
else
|
38
|
+
'NULLS LAST'
|
39
|
+
end
|
40
|
+
end
|
31
41
|
end
|
32
42
|
end
|
33
43
|
end
|
@@ -3,12 +3,11 @@ require 'tablets/data/processing/base'
|
|
3
3
|
module Tablets
|
4
4
|
class Data
|
5
5
|
module Processing
|
6
|
-
# Incapsulate relation pagination logic
|
6
|
+
# Incapsulate relation pagination logic
|
7
7
|
class Paginate < Tablets::Data::Processing::Base
|
8
|
-
# Applies :start and :length from params to relation
|
9
|
-
#
|
10
|
-
# :
|
11
|
-
# :start recalculated to match beginnning of page.
|
8
|
+
# Applies :start and :length from params to relation as offset and limit
|
9
|
+
# :length is optional. Default value is used if no length is provided
|
10
|
+
# :start recalculated to match beginnning of page
|
12
11
|
def apply(relation)
|
13
12
|
relation.offset(offset).limit(per_page)
|
14
13
|
end
|
data/lib/tablets/data/query.rb
CHANGED
@@ -6,26 +6,26 @@ require 'tablets/data/processing/order'
|
|
6
6
|
|
7
7
|
module Tablets
|
8
8
|
class Data
|
9
|
-
# Incapsulates database query
|
9
|
+
# Incapsulates database query
|
10
10
|
class Query
|
11
|
-
# Initializes query with relation, params and columns
|
11
|
+
# Initializes query with relation, params and columns
|
12
12
|
def initialize(relation, params, columns)
|
13
13
|
@relation = relation
|
14
14
|
@params = params
|
15
15
|
@columns = columns
|
16
16
|
end
|
17
17
|
|
18
|
-
# Applies all processings on relation and returns it
|
18
|
+
# Applies all processings on relation and returns it
|
19
19
|
def fetch
|
20
20
|
paginate filter order relation
|
21
21
|
end
|
22
22
|
|
23
|
-
# Returns total records count before filter and pagination is applied
|
23
|
+
# Returns total records count before filter and pagination is applied
|
24
24
|
def total
|
25
25
|
relation.count(:all)
|
26
26
|
end
|
27
27
|
|
28
|
-
# Returns records count after filter is applied but before pagination
|
28
|
+
# Returns records count after filter is applied but before pagination
|
29
29
|
def filtered
|
30
30
|
filter(relation).count(:all)
|
31
31
|
end
|
@@ -34,21 +34,21 @@ module Tablets
|
|
34
34
|
|
35
35
|
attr_reader :relation, :params, :columns
|
36
36
|
|
37
|
-
# Applies order processing
|
37
|
+
# Applies order processing
|
38
38
|
def order(records)
|
39
39
|
return records unless params[:order].present?
|
40
40
|
|
41
41
|
Tablets::Data::Processing::Order.apply(params, columns, records)
|
42
42
|
end
|
43
43
|
|
44
|
-
# Applies filter processing
|
44
|
+
# Applies filter processing
|
45
45
|
def filter(records)
|
46
46
|
return records unless params[:search].present?
|
47
47
|
|
48
48
|
Tablets::Data::Processing::Filter.apply(params, columns, records)
|
49
49
|
end
|
50
50
|
|
51
|
-
# Applies paginate processing
|
51
|
+
# Applies paginate processing
|
52
52
|
def paginate(records)
|
53
53
|
return records if params[:length] == '-1'
|
54
54
|
|
data/lib/tablets/data.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
require 'tablets/data/query'
|
2
2
|
|
3
3
|
module Tablets
|
4
|
-
# Responsible to fetch and prepare data for jquery-datatables
|
4
|
+
# Responsible to fetch and prepare data for jquery-datatables
|
5
5
|
class Data
|
6
|
-
# Initializes data with tablet and params
|
6
|
+
# Initializes data with tablet and params
|
7
7
|
def initialize(tablet, params, controller)
|
8
8
|
@tablet = tablet
|
9
9
|
@params = params
|
10
10
|
@controller = controller
|
11
11
|
end
|
12
12
|
|
13
|
-
# Prepares data to render as json
|
13
|
+
# Prepares data to render as json
|
14
14
|
def as_json(_options = {})
|
15
15
|
{
|
16
16
|
draw: params[:draw].to_i,
|
@@ -25,32 +25,32 @@ module Tablets
|
|
25
25
|
|
26
26
|
attr_reader :params, :tablet, :controller
|
27
27
|
|
28
|
-
# Initializes query with concrete relation
|
28
|
+
# Initializes query with concrete relation
|
29
29
|
def query
|
30
30
|
@query ||= Tablets::Data::Query.new(concrete_relation,
|
31
31
|
params,
|
32
32
|
tablet.columns)
|
33
33
|
end
|
34
34
|
|
35
|
-
# Relation with concretized parameters
|
35
|
+
# Relation with concretized parameters
|
36
36
|
def concrete_relation
|
37
37
|
tablet.relation(relation_params, controller)
|
38
38
|
end
|
39
39
|
|
40
|
-
# Relation params. Empty hash by default
|
40
|
+
# Relation params. Empty hash by default
|
41
41
|
def relation_params
|
42
42
|
params[:params] || {}
|
43
43
|
end
|
44
44
|
|
45
|
-
# Fetching records and applies process tablet callback on it
|
45
|
+
# Fetching records and applies process tablet callback on it
|
46
46
|
def records
|
47
47
|
@records ||= tablet.process(query.fetch)
|
48
48
|
end
|
49
49
|
|
50
|
-
# Fetching columns data for each row using column[:data] value
|
51
|
-
# If column[:data] is symbol send it to the record
|
52
|
-
# If column[:data] is proc calls it on the record
|
53
|
-
# Also appends details
|
50
|
+
# Fetching columns data for each row using column[:data] value:
|
51
|
+
# * If column[:data] is symbol send it to the record
|
52
|
+
# * If column[:data] is proc calls it on the record
|
53
|
+
# Also appends details
|
54
54
|
def data
|
55
55
|
records.map do |record|
|
56
56
|
data = tablet.columns.map.with_index do |column, index|
|
@@ -64,7 +64,7 @@ module Tablets
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
-
# Returns single cell value, for specified record and for specified column
|
67
|
+
# Returns single cell value, for specified record and for specified column
|
68
68
|
def cell(record, column)
|
69
69
|
case column[:data]
|
70
70
|
when Symbol then record.send(column[:data])
|
data/lib/tablets/engine.rb
CHANGED
@@ -2,17 +2,17 @@ require 'rails'
|
|
2
2
|
|
3
3
|
module Tablets
|
4
4
|
module Global
|
5
|
-
# Responsible to make application configurable
|
5
|
+
# Responsible to make application configurable
|
6
6
|
module Configurator
|
7
7
|
attr_accessor :options
|
8
8
|
attr_accessor :tablets_dir
|
9
9
|
|
10
|
-
# Setup configuration. Used in initializers
|
10
|
+
# Setup configuration. Used in initializers
|
11
11
|
def setup
|
12
12
|
yield self
|
13
13
|
end
|
14
14
|
|
15
|
-
# Initialize default config values
|
15
|
+
# Initialize default config values
|
16
16
|
def self.extended(base)
|
17
17
|
base.options = DEFAULT_OPTIONS
|
18
18
|
base.tablets_dir = File.expand_path('app/tablets', Rails.root)
|
@@ -1,19 +1,20 @@
|
|
1
1
|
module Tablets
|
2
2
|
module Global
|
3
|
-
# Tablets loader
|
3
|
+
# Tablets loader
|
4
|
+
# Manages files loading from app tablets directory
|
4
5
|
module Loader
|
5
|
-
# Unloads tablet
|
6
|
+
# Unloads tablet
|
6
7
|
def unload!
|
7
8
|
@tablets = nil
|
8
9
|
end
|
9
10
|
|
10
|
-
# Checks if tablet is loading
|
11
|
-
# Tablets is not loaded only if tablets is nil
|
11
|
+
# Checks if tablet is loading
|
12
|
+
# Tablets is not loaded only if tablets is nil
|
12
13
|
def loaded?
|
13
14
|
!@tablets.nil?
|
14
15
|
end
|
15
16
|
|
16
|
-
# Loads tablets files
|
17
|
+
# Loads tablets files
|
17
18
|
def load!
|
18
19
|
@tablets = {}
|
19
20
|
files.each { |file| load file }
|
@@ -21,12 +22,12 @@ module Tablets
|
|
21
22
|
|
22
23
|
private
|
23
24
|
|
24
|
-
# Load paths
|
25
|
+
# Load paths
|
25
26
|
def load_paths
|
26
27
|
[Tablets.tablets_dir]
|
27
28
|
end
|
28
29
|
|
29
|
-
# Prepares files list using load paths
|
30
|
+
# Prepares files list using load paths
|
30
31
|
def files
|
31
32
|
load_paths.flatten.compact.uniq.flat_map do |path|
|
32
33
|
Dir["#{path}/**/*.rb"]
|
data/lib/tablets/global/store.rb
CHANGED
@@ -2,8 +2,8 @@ require 'tablets/tablet'
|
|
2
2
|
|
3
3
|
module Tablets
|
4
4
|
module Global
|
5
|
-
# Tables store
|
6
|
-
# Expects @tablets to be defined elsewhere
|
5
|
+
# Tables store
|
6
|
+
# Expects @tablets to be defined elsewhere
|
7
7
|
module Store
|
8
8
|
extend Forwardable
|
9
9
|
|
@@ -11,7 +11,7 @@ module Tablets
|
|
11
11
|
|
12
12
|
def_delegators :tablets, :[]
|
13
13
|
|
14
|
-
# Initializes tablet and put tablet into store
|
14
|
+
# Initializes tablet and put tablet into store
|
15
15
|
#
|
16
16
|
# Tablets.register(:user) do
|
17
17
|
# # Tablet configuration.
|
data/lib/tablets/railtie.rb
CHANGED
@@ -5,13 +5,13 @@ require 'tablets/view_helpers'
|
|
5
5
|
module Tablets
|
6
6
|
# Rails hooks.
|
7
7
|
class Railtie < ::Rails::Railtie
|
8
|
-
# Allows tablets view helper to be used anywhere in rails views
|
8
|
+
# Allows tablets view helper to be used anywhere in rails views
|
9
9
|
initializer 'tablets.view_helpers' do
|
10
10
|
ActionView::Base.send :include, Tablets::ViewHelpers
|
11
11
|
end
|
12
12
|
|
13
|
-
# Manages tablets loading and cleanup
|
14
|
-
# Also ensures tablets reloading in development environment
|
13
|
+
# Manages tablets loading and cleanup
|
14
|
+
# Also ensures tablets reloading in development environment
|
15
15
|
initializer 'tablets.reloader' do |app|
|
16
16
|
if app.config.reload_classes_only_on_change
|
17
17
|
ActionDispatch::Reloader.to_prepare(prepend: true) { Tablets.unload! }
|
data/lib/tablets/renderer.rb
CHANGED
@@ -3,15 +3,15 @@ require 'active_support/core_ext/object/blank'
|
|
3
3
|
require 'tablets/engine'
|
4
4
|
|
5
5
|
module Tablets
|
6
|
-
# Prepares markup. Renders table without any data
|
6
|
+
# Prepares markup. Renders table without any data
|
7
7
|
class Renderer
|
8
|
-
# Initializes renderer with tablet and params
|
8
|
+
# Initializes renderer with tablet and params
|
9
9
|
def initialize(tablet, params = {})
|
10
10
|
@tablet = tablet
|
11
11
|
@params = params
|
12
12
|
end
|
13
13
|
|
14
|
-
# Renders table in view_context
|
14
|
+
# Renders table in view_context
|
15
15
|
def render(view_context)
|
16
16
|
view_context.render(partial: 'tablets/tablet', locals: locals)
|
17
17
|
end
|
@@ -20,7 +20,7 @@ module Tablets
|
|
20
20
|
|
21
21
|
attr_reader :params, :tablet
|
22
22
|
|
23
|
-
# Prepares locals for tablet partial
|
23
|
+
# Prepares locals for tablet partial
|
24
24
|
def locals
|
25
25
|
{
|
26
26
|
id: id,
|
@@ -31,7 +31,7 @@ module Tablets
|
|
31
31
|
}
|
32
32
|
end
|
33
33
|
|
34
|
-
# Id for HTML container. Includes name and params with values
|
34
|
+
# Id for HTML container. Includes name and params with values
|
35
35
|
#
|
36
36
|
# renderer = Tablets::Renderer.new(:posts, user_id: 1)
|
37
37
|
# renderer.id #=> 'posts_user_id_1'
|
@@ -47,7 +47,7 @@ module Tablets
|
|
47
47
|
.merge(tablet.options)
|
48
48
|
end
|
49
49
|
|
50
|
-
# Calculates column options and applies options on top
|
50
|
+
# Calculates column options and applies options on top
|
51
51
|
#
|
52
52
|
# ...
|
53
53
|
# columns do
|
@@ -77,7 +77,7 @@ module Tablets
|
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
80
|
-
# Returns path to data
|
80
|
+
# Returns path to data
|
81
81
|
def data_path
|
82
82
|
Tablets::Engine.routes.url_helpers.data_path(tablet.name)
|
83
83
|
end
|
data/lib/tablets/tablet.rb
CHANGED
@@ -1,48 +1,48 @@
|
|
1
1
|
require 'tablets/utils/config'
|
2
2
|
|
3
3
|
module Tablets
|
4
|
-
# Incapsulates tablet related information
|
4
|
+
# Incapsulates tablet related information
|
5
5
|
class Tablet
|
6
6
|
attr_reader :name
|
7
7
|
|
8
|
-
# Initializes tablet with name, callbacks fed with block
|
8
|
+
# Initializes tablet with name, callbacks fed with block
|
9
9
|
def initialize(name, &block)
|
10
10
|
@name = name
|
11
11
|
@config = Tablets::Utils::Config.new(&block)
|
12
12
|
end
|
13
13
|
|
14
|
-
# Returns general jquery-datatable configuration overrides
|
15
|
-
# By default returns empty options
|
14
|
+
# Returns general jquery-datatable configuration overrides
|
15
|
+
# By default returns empty options
|
16
16
|
def options
|
17
17
|
call(:options) { {} }
|
18
18
|
end
|
19
19
|
|
20
|
-
# Determines is user authorized
|
21
|
-
# By default returns true
|
20
|
+
# Determines is user authorized
|
21
|
+
# By default returns true
|
22
22
|
def authorize(controller)
|
23
23
|
call(:authorize, controller) { true }
|
24
24
|
end
|
25
25
|
|
26
|
-
# Allows to make additional processing before records would be used
|
27
|
-
# By default returns records
|
26
|
+
# Allows to make additional processing before records would be used
|
27
|
+
# By default returns records
|
28
28
|
def process(records)
|
29
29
|
call(:process, records) { records }
|
30
30
|
end
|
31
31
|
|
32
|
-
# Returns details HTML for the record
|
33
|
-
# By default returns nil
|
32
|
+
# Returns details HTML for the record
|
33
|
+
# By default returns nil
|
34
34
|
def details(record)
|
35
35
|
call(:details, record) { nil }
|
36
36
|
end
|
37
37
|
|
38
|
-
# Returns columns definitions
|
39
|
-
# By default deduct columns from relation
|
38
|
+
# Returns columns definitions
|
39
|
+
# By default deduct columns from relation
|
40
40
|
def columns
|
41
41
|
call(:columns) { deduct_columns_from_relation }
|
42
42
|
end
|
43
43
|
|
44
|
-
# Returns database relation to fetch data
|
45
|
-
# By default tries to deduct class from tablet name
|
44
|
+
# Returns database relation to fetch data
|
45
|
+
# By default tries to deduct class from tablet name
|
46
46
|
def relation(params = {}, controller = nil)
|
47
47
|
if defined? @name.singularize.camelize.constantize
|
48
48
|
call(:relation, params, controller) do
|
@@ -53,19 +53,19 @@ module Tablets
|
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
-
# Returns additional data returned to client
|
56
|
+
# Returns additional data returned to client
|
57
57
|
def payload
|
58
58
|
call(:payload) { {} }
|
59
59
|
end
|
60
60
|
|
61
|
-
# Checks if config has callback with specified name
|
61
|
+
# Checks if config has callback with specified name
|
62
62
|
def has?(name)
|
63
63
|
@config.has?(name)
|
64
64
|
end
|
65
65
|
|
66
66
|
private
|
67
67
|
|
68
|
-
# Returns default columns definititions, deducted from relation
|
68
|
+
# Returns default columns definititions, deducted from relation
|
69
69
|
def deduct_columns_from_relation
|
70
70
|
relation.columns.map(&:name).map do |name|
|
71
71
|
{
|
@@ -76,8 +76,7 @@ module Tablets
|
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
|
-
# Calls callback
|
80
|
-
# Clarifies error message on error.
|
79
|
+
# Calls callback and clarifies error message on error
|
81
80
|
def call(callback, *params, &block)
|
82
81
|
@config.call(callback, *params, &block)
|
83
82
|
rescue ArgumentError
|
data/lib/tablets/utils/config.rb
CHANGED
@@ -3,45 +3,45 @@ require 'active_support/core_ext/object/blank'
|
|
3
3
|
module Tablets
|
4
4
|
module Utils
|
5
5
|
# Config utility. Allows to write configs in declarative form.
|
6
|
-
# And fetch values with defaults
|
6
|
+
# And fetch values with defaults:
|
7
7
|
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
8
|
+
# config = Config.new do
|
9
|
+
# some_var 32
|
10
|
+
# another_var 42
|
11
11
|
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
12
|
+
# some_callback do |arg|
|
13
|
+
# do_something_with arg
|
14
|
+
# end
|
15
|
+
# end
|
16
16
|
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
17
|
+
# config.get(:some_var) #=> 32
|
18
|
+
# config.get(:non_existent_var) #=> ArgumentError
|
19
|
+
# config.get(:non_existent_var, 'default') #=> 'default'
|
20
20
|
#
|
21
|
-
#
|
21
|
+
# config.call(:some_callback, 'my_arg') { |arg| default_action arg }
|
22
22
|
#
|
23
23
|
class Config
|
24
|
-
# Initializes config with block
|
25
|
-
# Block is optional and can be applied later
|
24
|
+
# Initializes config with block
|
25
|
+
# Block is optional and can be applied later
|
26
26
|
def initialize(&block)
|
27
27
|
@hash = {}
|
28
28
|
|
29
29
|
apply(&block) unless block.nil?
|
30
30
|
end
|
31
31
|
|
32
|
-
# Executes block in config context
|
32
|
+
# Executes block in config context
|
33
33
|
def apply(&block)
|
34
34
|
instance_eval(&block)
|
35
35
|
end
|
36
36
|
|
37
|
-
# Checks if value or callback is defined with specified name
|
37
|
+
# Checks if value or callback is defined with specified name
|
38
38
|
def has?(name)
|
39
39
|
@hash[name].present?
|
40
40
|
end
|
41
41
|
|
42
|
-
# Returns value
|
43
|
-
# If no value defined, returns default
|
44
|
-
# If no default raises ArgumentError
|
42
|
+
# Returns value
|
43
|
+
# If no value defined, returns default
|
44
|
+
# If no default raises ArgumentError
|
45
45
|
def get(name, default = nil, &default_block)
|
46
46
|
value = @hash[name][0] if @hash[name]
|
47
47
|
|
@@ -56,9 +56,9 @@ module Tablets
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
-
# Calls callback
|
60
|
-
# If no calbback defined, calls default
|
61
|
-
# If no default raises ArgumentError
|
59
|
+
# Calls callback
|
60
|
+
# If no calbback defined, calls default
|
61
|
+
# If no default raises ArgumentError
|
62
62
|
def call(name, *params, &default)
|
63
63
|
callback = @hash[name][0] if @hash[name]
|
64
64
|
|
@@ -73,7 +73,7 @@ module Tablets
|
|
73
73
|
|
74
74
|
private
|
75
75
|
|
76
|
-
# Gathers all calls
|
76
|
+
# Gathers all calls
|
77
77
|
def method_missing(name, *args, &block)
|
78
78
|
@hash[name] = [*args, block]
|
79
79
|
end
|
@@ -3,46 +3,46 @@ require 'active_record'
|
|
3
3
|
|
4
4
|
module Tablets
|
5
5
|
module Utils
|
6
|
-
# Resposible for building database specific search conditions
|
6
|
+
# Resposible for building database specific search conditions
|
7
7
|
class SearchConditionBuilder
|
8
8
|
attr_reader :column, :query
|
9
9
|
|
10
|
-
# Initializes builder
|
10
|
+
# Initializes builder
|
11
11
|
def initialize(column, query)
|
12
12
|
@column = column
|
13
13
|
@query = query
|
14
14
|
end
|
15
15
|
|
16
|
-
# Builds search condition for specific db type
|
16
|
+
# Builds search condition for specific db type
|
17
17
|
def build
|
18
18
|
sanitize "(#{prepared_column} #{like} ?)", "%#{query}%"
|
19
19
|
end
|
20
20
|
|
21
21
|
private
|
22
22
|
|
23
|
-
# Sanitizes sql expression with params
|
23
|
+
# Sanitizes sql expression with params
|
24
24
|
def sanitize(sql, *params)
|
25
25
|
ActiveRecord::Base.send(:sanitize_sql_array, [sql, *params])
|
26
26
|
end
|
27
27
|
|
28
|
-
# Returns column prepared for using in SQL statement
|
28
|
+
# Returns column prepared for using in SQL statement
|
29
29
|
def prepared_column
|
30
30
|
cast quote @column
|
31
31
|
end
|
32
32
|
|
33
|
-
# Cast column to string
|
33
|
+
# Cast column to string
|
34
34
|
def cast(column)
|
35
35
|
"CAST (#{column} AS #{string_type})"
|
36
36
|
end
|
37
37
|
|
38
|
-
# Quotes column name
|
38
|
+
# Quotes column name
|
39
39
|
def quote(column)
|
40
40
|
column.split('.').map do |component|
|
41
41
|
ActiveRecord::Base.connection.quote_column_name component
|
42
42
|
end.join('.')
|
43
43
|
end
|
44
44
|
|
45
|
-
# Returns database specific like operator
|
45
|
+
# Returns database specific like operator
|
46
46
|
def like
|
47
47
|
case db_adapter
|
48
48
|
when :postgresql then 'ILIKE'
|
@@ -51,7 +51,7 @@ module Tablets
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
-
# Returns database specific string type
|
54
|
+
# Returns database specific string type
|
55
55
|
def string_type
|
56
56
|
case db_adapter
|
57
57
|
when :postgresql then 'VARCHAR'
|
@@ -60,7 +60,7 @@ module Tablets
|
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
|
-
# Retrieves rails database adapter
|
63
|
+
# Retrieves rails database adapter
|
64
64
|
def db_adapter
|
65
65
|
@db_adapter ||=
|
66
66
|
ActiveRecord::Base.configurations[Rails.env]['adapter'].to_sym
|
data/lib/tablets/version.rb
CHANGED
data/lib/tablets/view_helpers.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
require 'tablets/renderer'
|
2
2
|
|
3
3
|
module Tablets
|
4
|
-
# View helpers included to Rails
|
4
|
+
# View helpers included to Rails
|
5
5
|
module ViewHelpers
|
6
|
-
# Finds tablet by name and renders is with params in current view context
|
6
|
+
# Finds tablet by name and renders is with params in current view context
|
7
7
|
#
|
8
8
|
# <%= render_tablet :posts, user_id: @user.id %>
|
9
9
|
#
|
data/lib/tablets.rb
CHANGED
@@ -2,7 +2,7 @@ require 'tablets/global/loader'
|
|
2
2
|
require 'tablets/global/store'
|
3
3
|
require 'tablets/global/configurator'
|
4
4
|
|
5
|
-
# Top level tablets module
|
5
|
+
# Top level tablets module extended with global tablets methods
|
6
6
|
module Tablets
|
7
7
|
autoload :Data, 'tablets/data'
|
8
8
|
|
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.
|
4
|
+
version: 0.3.10
|
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-
|
11
|
+
date: 2015-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -164,22 +164,22 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
164
164
|
version: '0'
|
165
165
|
requirements: []
|
166
166
|
rubyforge_project:
|
167
|
-
rubygems_version: 2.4.
|
167
|
+
rubygems_version: 2.4.6
|
168
168
|
signing_key:
|
169
169
|
specification_version: 4
|
170
170
|
summary: Wrapper around jquery-datatables.
|
171
171
|
test_files:
|
172
|
-
- spec/spec_helper.rb
|
173
172
|
- spec/lib/tablets/data/processing/base_spec.rb
|
174
|
-
- spec/lib/tablets/data/processing/paginate_spec.rb
|
175
|
-
- spec/lib/tablets/data/processing/order_spec.rb
|
176
173
|
- spec/lib/tablets/data/processing/filter_spec.rb
|
174
|
+
- spec/lib/tablets/data/processing/order_spec.rb
|
175
|
+
- spec/lib/tablets/data/processing/paginate_spec.rb
|
177
176
|
- spec/lib/tablets/data/query_spec.rb
|
178
|
-
- spec/lib/tablets/global/store_spec.rb
|
179
177
|
- spec/lib/tablets/global/configurator_spec.rb
|
180
178
|
- spec/lib/tablets/global/loader_spec.rb
|
181
|
-
- spec/lib/tablets/
|
182
|
-
- spec/lib/tablets/utils/search_condition_builder_spec.rb
|
183
|
-
- spec/lib/tablets/utils/config_spec.rb
|
184
|
-
- spec/lib/tablets/tablet_spec.rb
|
179
|
+
- spec/lib/tablets/global/store_spec.rb
|
185
180
|
- spec/lib/tablets/renderer_spec.rb
|
181
|
+
- spec/lib/tablets/tablet_spec.rb
|
182
|
+
- spec/lib/tablets/utils/config_spec.rb
|
183
|
+
- spec/lib/tablets/utils/search_condition_builder_spec.rb
|
184
|
+
- spec/lib/tablets/view_helpers_spec.rb
|
185
|
+
- spec/spec_helper.rb
|