effective_datatables 2.0.6 → 2.1.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: ee352555dfda17eb3ac104a32f40da176aa25e92
4
- data.tar.gz: 9d53b17a19f244c52cabcb9fef855a628094a2f9
3
+ metadata.gz: 0c2b41d68aae71ba0779f8dc2f8144dda6b5a892
4
+ data.tar.gz: 110fc6ea4011eca17caf7044ad0ef3801e5456f1
5
5
  SHA512:
6
- metadata.gz: 6bd13ccd81794f45ccc7aded59f899192eebd3e134263495d19254d6598267fad646c6bfdfec327f144fe58f7d52ba3909e1730cdabeb882905c24a1616c6554
7
- data.tar.gz: 8ad6860e06182a9c9078817e99a5683e11e5e98a7999406553c3a7cf5df97aef78a4f5aadade94a951a94ab21a8422ccbb5b08bac9ca4f31f1c4f426661bb2de
6
+ metadata.gz: 06e528fa15d587e5ee09d3a934a37add14cdc4e1b2fc26981a7cd7c1e5e3d3d7f15a4afdfb2c7458646804f74275f2f44b7d19d30aa267721d34b3cfdeea5e67
7
+ data.tar.gz: cbf3aad24a9878efa02c50f97f5c3e0dae9f43b532b74aec9f327466cad905f6f576f6b329a1fa657d8b38c579fcd4dfed866354dd0363a93425a867090de4f5
@@ -7,7 +7,7 @@ module Effective
7
7
  @datatable = find_datatable(params[:id]).try(:new, params[:attributes])
8
8
  @datatable.view = view_context if !@datatable.nil?
9
9
 
10
- EffectiveDatatables.authorized?(self, :index, @datatable.try(:collection_class) || Effective::Datatable)
10
+ EffectiveDatatables.authorized?(self, :index, @datatable.try(:collection_class) || @datatable.try(:class))
11
11
 
12
12
  respond_to do |format|
13
13
  format.html
@@ -23,7 +23,7 @@ module Effective
23
23
  if [:string, :text].include?(order_column[:type]) && order_column[:sql_as_column] != true
24
24
  collection.order("COALESCE(#{order_column[:column]}, '') #{order_direction}")
25
25
  else
26
- collection.order("#{order_column[:column]} #{order_direction} NULLS #{order_direction == 'ASC' ? 'LAST' : 'FIRST'}")
26
+ collection.order("#{order_column[:column]} #{order_direction} NULLS LAST")
27
27
  end
28
28
  end
29
29
 
@@ -98,13 +98,13 @@ module Effective
98
98
  collection
99
99
  end
100
100
  when :integer
101
- collection.public_send(sql_op, "#{column} = :term", term: term.to_i)
101
+ collection.public_send(sql_op, "#{column} = :term", term: term.gsub(/\D/, '').to_i)
102
102
  when :year
103
103
  collection.public_send(sql_op, "EXTRACT(YEAR FROM #{column}) = :term", term: term.to_i)
104
104
  when :price
105
105
  price_in_cents = (term.gsub(/[^0-9|\.]/, '').to_f * 100.0).to_i
106
106
  collection.public_send(sql_op, "#{column} = :term", term: price_in_cents)
107
- when :currency
107
+ when :currency, :decimal
108
108
  collection.public_send(sql_op, "#{column} = :term", term: term.gsub(/[^0-9|\.]/, '').to_f)
109
109
  else
110
110
  collection.public_send(sql_op, "#{column} = :term", term: term)
@@ -2,7 +2,7 @@ module Effective
2
2
  class Datatable
3
3
  attr_accessor :total_records, :display_records, :view, :attributes
4
4
 
5
- delegate :render, :link_to, :mail_to, :number_to_currency, :to => :@view
5
+ delegate :render, :controller, :link_to, :mail_to, :number_to_currency, :to => :@view
6
6
 
7
7
  include Effective::EffectiveDatatable::Dsl
8
8
  extend Effective::EffectiveDatatable::Dsl::ClassMethods
@@ -36,9 +36,9 @@ module Effective
36
36
  end
37
37
 
38
38
  def actions_column(options = {}, proc = nil, &block)
39
- show = options.fetch(:show, false)
40
- edit = options.fetch(:edit, true)
41
- destroy = options.fetch(:destroy, true)
39
+ show = options.fetch(:show, (EffectiveDatatables.actions_column[:show] rescue false))
40
+ edit = options.fetch(:edit, (EffectiveDatatables.actions_column[:edit] rescue false))
41
+ destroy = options.fetch(:destroy, (EffectiveDatatables.actions_column[:destroy] rescue false))
42
42
  name = options.fetch(:name, 'actions')
43
43
 
44
44
  opts = {
@@ -124,13 +124,25 @@ module Effective
124
124
  when :belongs_to
125
125
  {
126
126
  type: :select,
127
- values: Proc.new { belongs_to[:klass].all.map { |obj| [obj.to_s, obj.id] }.sort { |x, y| x[0] <=> y[0] } }
127
+ values: (
128
+ if belongs_to[:klass].respond_to?(:datatables_filter)
129
+ Proc.new { belongs_to[:klass].datatables_filter }
130
+ else
131
+ Proc.new { belongs_to[:klass].all.map { |obj| [obj.to_s, obj.id] }.sort { |x, y| x[0] <=> y[0] } }
132
+ end
133
+ )
128
134
  }
129
135
  when :has_many
130
136
  {
131
137
  type: :select,
132
138
  multiple: true,
133
- values: Proc.new { has_many[:klass].all.map { |obj| [obj.to_s, obj.id] }.sort { |x, y| x[0] <=> y[0] } }
139
+ values: (
140
+ if has_many[:klass].respond_to?(:datatables_filter)
141
+ Proc.new { has_many[:klass].datatables_filter }
142
+ else
143
+ Proc.new { has_many[:klass].all.map { |obj| [obj.to_s, obj.id] }.sort { |x, y| x[0] <=> y[0] } }
144
+ end
145
+ )
134
146
  }
135
147
  when :effective_roles
136
148
  {type: :select, values: EffectiveRoles.roles}
@@ -71,6 +71,20 @@ module Effective
71
71
  }
72
72
  locals.merge!(opts[:partial_locals]) if opts[:partial_locals]
73
73
 
74
+ if active_record_collection?
75
+ if locals[:show_action] == :authorize
76
+ locals[:show_action] = (EffectiveDatatables.authorized?(controller, :show, collection_class) rescue false)
77
+ end
78
+
79
+ if locals[:edit_action] == :authorize
80
+ locals[:edit_action] = (EffectiveDatatables.authorized?(controller, :edit, collection_class) rescue false)
81
+ end
82
+
83
+ if locals[:destroy_action] == :authorize
84
+ locals[:destroy_action] = (EffectiveDatatables.authorized?(controller, :destroy, collection_class) rescue false)
85
+ end
86
+ end
87
+
74
88
  rendered[name] = (render(
75
89
  :partial => opts[:partial],
76
90
  :as => opts[:partial_local],
@@ -117,6 +131,14 @@ module Effective
117
131
  number_to_currency(value / 100.0)
118
132
  when :currency
119
133
  number_to_currency(value || 0)
134
+ when :integer
135
+ if EffectiveDatatables.integer_format.kind_of?(Symbol)
136
+ view.instance_exec { public_send(EffectiveDatatables.integer_format, value) }
137
+ elsif EffectiveDatatables.integer_format.respond_to?(:call)
138
+ view.instance_exec { EffectiveDatatables.integer_format.call(value) }
139
+ else
140
+ value
141
+ end
120
142
  else
121
143
  value
122
144
  end
@@ -1,14 +1,17 @@
1
1
  - if show_action
2
2
  - url = (polymorphic_path([*controller_namespace, resource]) rescue nil) || (polymorphic_path(resource) rescue nil)
3
- %a{href: (url || '#'), title: 'Show'}
4
- %span.glyphicon.glyphicon-eye-open
3
+ - if url.present?
4
+ %a{href: url, title: 'Show'}
5
+ %span.glyphicon.glyphicon-eye-open
5
6
 
6
7
  - if edit_action
7
8
  - url = (edit_polymorphic_path([*controller_namespace, resource]) rescue nil) || (edit_polymorphic_path(resource) rescue nil)
8
- %a{href: (url || '#'), title: 'Edit'}
9
- %span.glyphicon.glyphicon-edit
9
+ - if url.present?
10
+ %a{href: url, title: 'Edit'}
11
+ %span.glyphicon.glyphicon-edit
10
12
 
11
13
  - if destroy_action
12
14
  - url = (polymorphic_path([*controller_namespace, resource]) rescue nil) || (polymorphic_path(resource) rescue nil)
13
- %a{href: (url || '#'), title: 'Delete', data: {method: :delete, confirm: 'Are you sure?'}}
14
- %span.glyphicon.glyphicon-trash
15
+ - if url.present?
16
+ %a{href: url, title: 'Delete', data: {method: :delete, confirm: 'Are you sure?'}}
17
+ %span.glyphicon.glyphicon-trash
@@ -7,7 +7,10 @@ module EffectiveDatatables
7
7
  mattr_accessor :authorization_method
8
8
  mattr_accessor :date_format
9
9
  mattr_accessor :datetime_format
10
+ mattr_accessor :integer_format
11
+
10
12
  mattr_accessor :default_entries
13
+ mattr_accessor :actions_column # A Hash
11
14
 
12
15
  def self.setup
13
16
  yield self
@@ -1,3 +1,3 @@
1
1
  module EffectiveDatatables
2
- VERSION = '2.0.6'.freeze
2
+ VERSION = '2.1.0'.freeze
3
3
  end
@@ -23,10 +23,32 @@ EffectiveDatatables.setup do |config|
23
23
 
24
24
  # Date & DateTime Format
25
25
  # By default, format Date and DateTime values with the following
26
- config.date_format = "%Y-%m-%d"
27
- config.datetime_format = "%Y-%m-%d %H:%M"
26
+ config.date_format = '%Y-%m-%d'
27
+ config.datetime_format = '%Y-%m-%d %H:%M'
28
+
29
+ # Format integer columns with the following proc or function
30
+ config.integer_format = :number_with_delimiter
28
31
 
29
32
  # Default number of entries shown per page
30
33
  # Valid options are: 10, 25, 50, 100, 250, 1000, :all
31
34
  config.default_entries = 25
35
+
36
+ # When using the actions_column DSL method, apply the following behavior
37
+ # Valid values for each action are:
38
+ # true (always include a link to this action)
39
+ # false (never include a link to this action)
40
+ # :authorize (include link only if authorized - only works with ActiveRecord collections)
41
+ #
42
+ # When :authorize, a single check will be made via the above authorization_method
43
+ # to see if the current_user has permission to :show/:edit/:destroy the collection class, i.e. Post
44
+ # EffectiveDatatables.authorized? (:show || :edit || :destroy), Post
45
+ #
46
+ # For performance reasons, we only check the class once, not every individual resource.
47
+ # You can override this default by calling `actions_column show: false, edit: true, destroy: :authorize`
48
+ config.actions_column = {
49
+ show: :authorize,
50
+ edit: :authorize,
51
+ destroy: :authorize
52
+ }
53
+
32
54
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_datatables
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.6
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect