lolita 3.4.0 → 3.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lolita/configuration/column.rb +77 -42
- data/lib/lolita/system_configuration/base.rb +4 -0
- data/lib/lolita/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f81d378fd85598e5de4c672cb470af30b6cb90f
|
4
|
+
data.tar.gz: 33b6055aaa7dbee4965baddd4c6a46e2c72fb192
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 769ab380f0638ea6f64cf9a39bc1e11bcd1be74dc1762952cc6135e8fd1c1111aef963c17f033d3a475dd4a60bdebfb9d9442ab0ec7b8318d2379f7084e306c5
|
7
|
+
data.tar.gz: a7c55aa4efa03cc50f5c77bada37f27ac01dd6a21e4852f1aaf843e6a0b105dbdb6397f59a9bcebe2a3b6f72644f2cae93278bd120e073398dc26d1e2db771ed
|
@@ -1,28 +1,51 @@
|
|
1
1
|
module Lolita
|
2
2
|
module Configuration
|
3
|
+
# column in a list
|
4
|
+
#
|
5
|
+
# === Examples
|
6
|
+
# lolita do
|
7
|
+
# list do
|
8
|
+
# column :title, sortable: true
|
9
|
+
# column :full_name, sortable: 'first_name'
|
10
|
+
# column :is_public do
|
11
|
+
# formatter do |value, record, view|
|
12
|
+
# value ? 'Yes' : 'No'
|
13
|
+
# end
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# column do
|
17
|
+
# title "ID"
|
18
|
+
# name :id
|
19
|
+
# formatter do|value, record, view|
|
20
|
+
# view.link_to(value, view.url_for(action: 'edit', id: value))
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# column :updated_at, title: 'Date', formatter: '%d.%m.%Y., %H:%M'
|
25
|
+
# end
|
26
|
+
# end
|
3
27
|
class Column < Lolita::Configuration::Base
|
4
|
-
|
5
|
-
MAX_TEXT_SIZE=20
|
28
|
+
MAX_TEXT_SIZE = 20
|
6
29
|
attr_reader :list_association_name
|
7
|
-
lolita_accessor :name
|
8
|
-
|
9
|
-
def initialize(dbi
|
30
|
+
lolita_accessor :name, :title, :type, :options, :sortable, :association
|
31
|
+
|
32
|
+
def initialize(dbi, *args, &block)
|
10
33
|
set_and_validate_dbi(dbi)
|
11
|
-
|
12
|
-
|
34
|
+
set_attributes(*args)
|
35
|
+
instance_eval(&block) if block_given?
|
13
36
|
validate
|
14
37
|
normalize_attributes
|
15
38
|
detect_association
|
16
39
|
end
|
17
40
|
|
18
|
-
def list
|
41
|
+
def list(*args, &block)
|
19
42
|
if args && args.any? || block_given?
|
20
43
|
detect_association
|
21
|
-
list_association = args[0] && @dbi.associations[args[0].to_s.to_sym] ||
|
44
|
+
list_association = args[0] && @dbi.associations[args[0].to_s.to_sym] || association
|
22
45
|
list_dbi = list_association && Lolita::DBI::Base.create(list_association.klass)
|
23
|
-
|
46
|
+
fail Lolita::UnknownDBPError.new("DBI is not specified for list in column #{self}") unless list_dbi
|
24
47
|
@list_association_name = list_association.name
|
25
|
-
Lolita::LazyLoader.lazy_load(self
|
48
|
+
Lolita::LazyLoader.lazy_load(self, :@list, Lolita::Configuration::NestedList, list_dbi, self, association_name: list_association.name, &block)
|
26
49
|
else
|
27
50
|
@list
|
28
51
|
end
|
@@ -31,8 +54,8 @@ module Lolita
|
|
31
54
|
# Return value of column from given record. When record matches foreign key patter, then foreign key is used.
|
32
55
|
# In other cases it just ask for attribute with same name as column.
|
33
56
|
def value(record)
|
34
|
-
if
|
35
|
-
if
|
57
|
+
if association
|
58
|
+
if association.macro == :one && dbi.klass.respond_to?(:human_attribute_name)
|
36
59
|
dbi.klass.human_attribute_name(association.name)
|
37
60
|
# dbi.record(record.send(association.name)).title
|
38
61
|
elsif dbi.klass.respond_to?(:human_attribute_name)
|
@@ -41,20 +64,20 @@ module Lolita
|
|
41
64
|
"#{association.name} (#{record.send(association.name).count})"
|
42
65
|
end
|
43
66
|
else
|
44
|
-
record.send(
|
67
|
+
record.send(name)
|
45
68
|
end
|
46
69
|
end
|
47
70
|
|
48
|
-
def formatted_value(record,view)
|
49
|
-
|
71
|
+
def formatted_value(record, view)
|
72
|
+
formatter.with(value(record), record, view)
|
50
73
|
end
|
51
74
|
|
52
75
|
# Set/Get title. Getter return title what was set or ask for human_attribute_name to model.
|
53
|
-
def title(new_title=nil)
|
76
|
+
def title(new_title = nil)
|
54
77
|
if new_title
|
55
78
|
@title = new_title
|
56
79
|
end
|
57
|
-
Lolita::Utils.dynamic_string(@title, :
|
80
|
+
Lolita::Utils.dynamic_string(@title, default: @name && @dbi.klass.human_attribute_name(@name))
|
58
81
|
end
|
59
82
|
|
60
83
|
def sortable?
|
@@ -63,43 +86,55 @@ module Lolita
|
|
63
86
|
|
64
87
|
# Find if any of received sort options matches this column.
|
65
88
|
def current_sort_state(params)
|
66
|
-
@sortable && sort_pairs(params).
|
89
|
+
@sortable && sort_pairs(params).find { |pair| pair[0] == sort_by_name } || []
|
67
90
|
end
|
68
91
|
|
69
92
|
# Return string with sort options for column if column is sortable.
|
70
|
-
def sort_params
|
93
|
+
def sort_params(params)
|
71
94
|
if @sortable
|
72
95
|
pairs = sort_pairs(params)
|
73
96
|
found_pair = false
|
74
|
-
pairs.each_with_index{|pair,index|
|
75
|
-
if pair[0] ==
|
76
|
-
pairs[index][1] = pair[1] ==
|
97
|
+
pairs.each_with_index{|pair, index|
|
98
|
+
if pair[0] == sort_by_name
|
99
|
+
pairs[index][1] = pair[1] == 'asc' ? 'desc' : 'asc'
|
77
100
|
found_pair = true
|
78
101
|
end
|
79
102
|
}
|
80
103
|
unless found_pair
|
81
|
-
pairs << [
|
104
|
+
pairs << [sort_by_name, 'asc']
|
82
105
|
end
|
83
|
-
(pairs.map{|pair| pair.join(
|
106
|
+
(pairs.map { |pair| pair.join(',') }).join('|')
|
84
107
|
else
|
85
|
-
|
108
|
+
''
|
86
109
|
end
|
87
110
|
end
|
88
111
|
|
112
|
+
# returns value to sort by
|
113
|
+
# in default it will be column name, but you can specify it
|
114
|
+
# in field configuration
|
115
|
+
#
|
116
|
+
# === Examples
|
117
|
+
# list do
|
118
|
+
# field :name, sortable: 'some_table.first_name'
|
119
|
+
# end
|
120
|
+
def sort_by_name
|
121
|
+
@sortable.is_a?(TrueClass) ? name.to_s : @sortable.to_s
|
122
|
+
end
|
123
|
+
|
89
124
|
# Create array of sort information from params.
|
90
|
-
def sort_pairs
|
91
|
-
(params[:s] ||
|
125
|
+
def sort_pairs(params)
|
126
|
+
(params[:s] || '').split('|').map { |pair| pair.split(',') }
|
92
127
|
end
|
93
128
|
|
94
129
|
# Define format, for details see Lolita::Support::Formatter and Lolita::Support::Formater::Rails
|
95
|
-
def formatter(value=nil
|
130
|
+
def formatter(value = nil, &block)
|
96
131
|
if block_given?
|
97
|
-
@formatter=Lolita::Support::Formatter.new(value
|
132
|
+
@formatter = Lolita::Support::Formatter.new(value, &block)
|
98
133
|
elsif value || !@formatter
|
99
134
|
if value.kind_of?(Lolita::Support::Formatter)
|
100
|
-
@formatter=value
|
135
|
+
@formatter = value
|
101
136
|
else
|
102
|
-
@formatter=Lolita::Support::Formatter::Rails.new(value)
|
137
|
+
@formatter = Lolita::Support::Formatter::Rails.new(value)
|
103
138
|
end
|
104
139
|
end
|
105
140
|
@formatter
|
@@ -107,30 +142,30 @@ module Lolita
|
|
107
142
|
|
108
143
|
def formatter=(value)
|
109
144
|
if value.kind_of?(Lolita::Support::Formatter)
|
110
|
-
@formatter=value
|
145
|
+
@formatter = value
|
111
146
|
else
|
112
|
-
@formatter=Lolita::Support::Formatter::Rails.new(value)
|
147
|
+
@formatter = Lolita::Support::Formatter::Rails.new(value)
|
113
148
|
end
|
114
149
|
end
|
115
150
|
|
116
151
|
def set_attributes(*args)
|
117
152
|
options = args ? args.extract_options! : {}
|
118
153
|
if args[0].respond_to?(:field)
|
119
|
-
[:name
|
120
|
-
|
154
|
+
[:name, :type].each do |attr|
|
155
|
+
send(:"#{attr}=", args[0].send(attr))
|
121
156
|
end
|
122
157
|
elsif args[0]
|
123
158
|
self.name = args[0]
|
124
159
|
end
|
125
|
-
options.each do |attr_name,value|
|
126
|
-
|
160
|
+
options.each do |attr_name, value|
|
161
|
+
send(:"#{attr_name}=", value)
|
127
162
|
end
|
128
163
|
end
|
129
|
-
|
164
|
+
|
130
165
|
private
|
131
166
|
|
132
167
|
def detect_association
|
133
|
-
@association ||= dbi.associations[
|
168
|
+
@association ||= dbi.associations[name]
|
134
169
|
end
|
135
170
|
|
136
171
|
def normalize_attributes
|
@@ -138,8 +173,8 @@ module Lolita
|
|
138
173
|
end
|
139
174
|
|
140
175
|
def validate
|
141
|
-
|
142
|
-
|
176
|
+
fail Lolita::UnknownDBIError.new("DBI is not specified for column #{self}") unless dbi
|
177
|
+
fail ArgumentError.new('Column must have name.') unless name
|
143
178
|
end
|
144
179
|
end
|
145
180
|
end
|
@@ -64,6 +64,10 @@ module Lolita
|
|
64
64
|
else
|
65
65
|
Lolita.default_locale
|
66
66
|
end
|
67
|
+
if defined?(::I18n)
|
68
|
+
::I18n.locale = @locale
|
69
|
+
end
|
70
|
+
@locale
|
67
71
|
end
|
68
72
|
# Return default locale. First looks for defined default locale for Lolita, when not found than
|
69
73
|
# take first of defined #locales for Lolita, if there no defined locales for Lolita, than
|
data/lib/lolita/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lolita
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.4.
|
4
|
+
version: 3.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ITHouse (Latvia) and Arturs Meisters
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kaminari
|