azahara_schema 0.1.12 → 0.1.13

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af8c74a55165b64dd1028655871bbccd2df367faf7f8e140c2574d5e3823260c
4
- data.tar.gz: 2b066a64fe4b33863562dad9d13c38dad1cd73f5460368f057851ef3dc873ab7
3
+ metadata.gz: 6a38482119a210601a41eac9333f38109bba98ab82ba7da4e4a10e657338b1b6
4
+ data.tar.gz: 24457857de38a9e8ac27b74439279484ec45c1da150f3f4e3bf74639324659f8
5
5
  SHA512:
6
- metadata.gz: e68614299630ffafb7d870b3a8aea26df55ddccd8bea8ea5deaeebfcea6e1b2f76364c9b45a3b435ccaebf4b613f7ce824d52fb1e2721bc99866dab91394a784
7
- data.tar.gz: 22bd14b10428e52aa5d61ccd404cc8ae892264548842c780abcecdd7bb9f19b4a72eddedfe1c34c9e0d10940d72fde20683664c4485cfde0fbc04028838e3f60
6
+ metadata.gz: b76450d6a94770ed1ee49302872a070233ada6d01d2f28b890a74da2136fb0f45b57a866805734930d5c3e9e9badc8c4af944d9ed560572ec89097dbe7162cd0
7
+ data.tar.gz: acac345155a9445a757cb36506f8a9a97aa5106b0a3a9f3c81e057b4ce0563243c8ee58e80fa51bd3fef3394423b9cb6ec61f6f434bba3be6b61498daeb9ca92
@@ -26,5 +26,9 @@ module AzaharaSchema
26
26
  end
27
27
  end
28
28
 
29
+ def attribute_formatter_for(schema_or_model, **options)
30
+ AttributeFormatter.formatter_for(schema_or_model).new(schema_or_model, self, options)
31
+ end
32
+
29
33
  end
30
34
  end
@@ -0,0 +1,6 @@
1
+ <% if local_assigns.fetch(:filters, true) %>
2
+ <%= render 'azahara_schema/filters', schema: schema %>
3
+ <% end %>
4
+ <% schema.outputs.each do |output| %>
5
+ <%= render output %>
6
+ <% end %>
@@ -0,0 +1,121 @@
1
+ module AzaharaSchema
2
+ class AttributeFormatter
3
+
4
+ def self.default_formatter=(formatter_klass)
5
+ @default_formatter = formatter_klass
6
+ end
7
+
8
+ def self.default_formatter
9
+ @default_formatter || AzaharaSchema::AttributeFormatter
10
+ end
11
+
12
+ def self.formatter_for(schema_or_entity)
13
+ klass = schema_or_entity.class if !schema_or_entity.is_a?(Class)
14
+ klass = schema_or_entity.model if schema_or_entity.is_a?(::AzaharaSchema::Schema)
15
+ klass ||= schema_or_entity
16
+ klasses = [klass]
17
+ while klass != klass.base_class
18
+ klass = klass.superclass
19
+ klasses << klass
20
+ end
21
+ klasses.each do |kls|
22
+ schema_klass = "#{kls.name}Formatter".safe_constantize || "Formatters::#{kls.name}Formatter".safe_constantize
23
+ return schema_klass if schema_klass
24
+ end
25
+ default_formatter
26
+ end
27
+
28
+ attr_reader :model, :template
29
+
30
+ def initialize(schema_or_entity, template, **options)
31
+ @schema = schema_or_entity if schema_or_entity.is_a?(::AzaharaSchema::Schema)
32
+ @entity = schema_or_entity if schema_or_entity.is_a?(::ActiveRecord::Base)
33
+ @model = @schema ? @schema.model : (@entity ? schema_or_entity.class : schema_or_entity)
34
+ @options = options
35
+ @template = template
36
+ end
37
+
38
+ def new_path(**options)
39
+ template.new_polymorphic_path(model, options)
40
+ end
41
+
42
+ def show_path(entity, **options)
43
+ template.polymorphic_path(entity, options)
44
+ end
45
+
46
+ def icon_class_for_attribute(attribute)
47
+ 'fa'
48
+ end
49
+
50
+ def attribute_human_value(attribute, entity, **options)
51
+ val = attribute.value(entity)
52
+ case attribute.type
53
+ when 'love'
54
+ attribute.available_values.detect{|l, v| v == val }.try(:[], 0)
55
+ when 'list'
56
+ attribute.attribute_name.human_list_value(val, options)
57
+ else
58
+ val
59
+ end
60
+ end
61
+
62
+ def formatted_value(attribute, entity, **options)
63
+ real_formatter(attribute).format_value(attribute, attribute_human_value(attribute, entity), options)
64
+ end
65
+
66
+ def html_formatted_value(attribute, entity, **options)
67
+ format_value_html(attribute, attribute.value(entity), options)
68
+ end
69
+
70
+ def attribute_html_label(attribute, **options)
71
+ attribute.attribute_name.human(options)
72
+ end
73
+
74
+ def labeled_html_attribute_value(attribute, entity, **options)
75
+ template.content_tag('div', class: 'attribute') do
76
+ s = ''.html_safe
77
+ s << template.content_tag('div', attribute_html_label(attribute, options), class: 'label')
78
+ s << template.content_tag('div', format_value_html(attribute, attribute.value(entity), options), class: 'value')
79
+ s
80
+ end
81
+ end
82
+
83
+
84
+ def format_value(attribute, unformated_value, **options)
85
+ case attribute.type
86
+ when 'datetime'
87
+ l(unformated_value)
88
+ else
89
+ unformated_value
90
+ end
91
+ end
92
+
93
+ def format_value_html(attribute, unformated_value, **options)
94
+ formated_value = real_formatter(attribute).format_value(attribute, unformated_value, options)
95
+ end
96
+
97
+ def real_formatter(attribute)
98
+ if attribute.respond_to?(:attribute)
99
+ self.class.formatter_for(attribute.attribute.model).new(attribute.attribute.model, template, @options).real_formatter(attribute.attribute)
100
+ else
101
+ self
102
+ end
103
+ end
104
+
105
+ def real_formatter_and_attribute(attribute)
106
+ if attribute.respond_to?(:attribute)
107
+ self.class.formatter_for(attribute.attribute.model).new(attribute.attribute.model, template, @options).real_formatter_and_attribute(attribute.attribute)
108
+ else
109
+ [self, attribute]
110
+ end
111
+ end
112
+
113
+ def with_real_formatter_and_attribute(attribute, &block)
114
+ yield real_formatter_and_attribute(attribute)
115
+ end
116
+
117
+ private
118
+ delegate :l, :t, to: :template
119
+
120
+ end
121
+ end
@@ -7,8 +7,13 @@ module AzaharaSchema
7
7
  @attribute = attribute
8
8
  end
9
9
 
10
- def human
11
- I18n.t(i18n_scoped_key, default: i18n_fallback_keys)
10
+ def human(**options)
11
+ I18n.t(i18n_scoped_key, options.merge(default: i18n_fallback_keys))
12
+ end
13
+
14
+ def human_list_value(value, **options)
15
+ return '' unless value.present?
16
+ I18n.t(i18n_scoped_list_key(value.to_s), options.merge(default: i18n_list_fallback_keys(value)+[value.to_s]))
12
17
  end
13
18
 
14
19
  def model_i18n_key
@@ -19,8 +24,16 @@ module AzaharaSchema
19
24
  attribute.name
20
25
  end
21
26
 
27
+ def i18n_scope
28
+ 'activerecord.attributes.' + model_i18n_key.to_s
29
+ end
30
+
22
31
  def i18n_scoped_key
23
- ('activerecord.attributes.' + model_i18n_key.to_s + '.' + i18n_key.to_s).to_sym
32
+ (i18n_scope + '.' + i18n_key.to_s).to_sym
33
+ end
34
+
35
+ def i18n_scoped_list_key(value)
36
+ i18n_scope + '.' + i18n_key.to_s.pluralize + '.' + value.to_s
24
37
  end
25
38
 
26
39
  def i18n_fallback_keys
@@ -30,7 +43,18 @@ module AzaharaSchema
30
43
  keys.concat( parent_attr_name.i18n_fallback_keys )
31
44
  keys
32
45
  else
33
- [ i18n_scoped_key.to_sym ]
46
+ []
47
+ end
48
+ end
49
+
50
+ def i18n_list_fallback_keys(value)
51
+ if attribute.respond_to?(:attribute)
52
+ parent_attr_name = attribute.attribute.attribute_name
53
+ keys = [ parent_attr_name.i18n_scoped_list_key(value).to_sym ]
54
+ keys.concat( parent_attr_name.i18n_list_fallback_keys(value) )
55
+ keys
56
+ else
57
+ []
34
58
  end
35
59
  end
36
60
  end
@@ -0,0 +1,17 @@
1
+ # redefine cancan collection load to schema load
2
+ module AzaharaSchema
3
+ module CanCan
4
+ module ControllerResourcePatch
5
+
6
+ def load_collection
7
+ schema = ::AzaharaSchema::Schema.schema_for(resource_class)
8
+ if @options[:trough]
9
+ schema.add_filter(parent_name.to_s+'_id', '=', parent_resource.id)
10
+ end
11
+ schema.visibility_scope!(current_ability, authorization_action)
12
+ schema
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ module AzaharaSchema
2
+ module ControllerAdditions
3
+
4
+ def azahara_schema_index(**options)
5
+ resource_schema = self.class.cancan_resource_class.new(self).send(:collection_instance)
6
+ resource_schema.from_params(params)
7
+ respond_to do |format|
8
+ format.html
9
+ format.json {
10
+ json_result = {}
11
+ if params['_type'] == 'query'
12
+ json_result[:results] = resource_schema.entities.collect do |o|
13
+ {id: o.id, text: o.to_s, residence: o.person.residence.to_s}
14
+ end
15
+ elsif params['_type'] == 'count'
16
+ json_result = {count: resource_schema.entity_count}
17
+ else
18
+ json_result = resource_schema
19
+ end
20
+ render json: json_result
21
+ }
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -8,6 +8,7 @@ require 'azahara_schema/outputs'
8
8
  require 'azahara_schema/output'
9
9
  require 'azahara_schema/schema'
10
10
  require 'azahara_schema/model_schema'
11
+ require 'azahara_schema/attribute_formatter'
11
12
 
12
13
  module AzaharaSchema
13
14
  class Engine < ::Rails::Engine
@@ -18,6 +19,19 @@ module AzaharaSchema
18
19
  g.fixture_replacement :factory_girl, :dir => 'spec/factories'
19
20
  end
20
21
 
22
+ initializer 'azahara_schema.controller_additions' do
23
+ if Object.const_defined?('CanCan::ControllerResource')
24
+ require 'azahara_schema/cancan/controller_resource_patch'
25
+ ::CanCan::ControllerResource.send(:prepend, AzaharaSchema::CanCan::ControllerResourcePatch)
26
+
27
+ require 'azahara_schema/controller_additions'
28
+ ActiveSupport.on_load(:action_controller) do
29
+ include ::AzaharaSchema::ControllerAdditions
30
+ end
31
+ end
32
+
33
+ end
34
+
21
35
  config.to_prepare do
22
36
  ::ApplicationController.helper(AzaharaSchema::ApplicationHelper)
23
37
  end
@@ -1,5 +1,6 @@
1
1
  module AzaharaSchema
2
2
  class ModelSchema < Schema
3
+
3
4
  def initialize(**attrs)
4
5
  super(model, attrs)
5
6
  end
@@ -12,6 +13,14 @@ module AzaharaSchema
12
13
  @model ||= self.class.name.sub(/Schema/, '').constantize
13
14
  end
14
15
 
16
+ def visibility_scope!(ability, authorization_action=:index)
17
+ @entity_scope = model.accessible_by(ability, authorization_action)
18
+ end
19
+
20
+ def entity_scope
21
+ @entity_scope || super
22
+ end
23
+
15
24
  # dummy implementations for rewrite
16
25
  def uncollapsable_filters
17
26
  available_filters.select{|name, filter| always_visible_filters.include?(name) }
@@ -21,5 +30,10 @@ module AzaharaSchema
21
30
  available_filters.select{|name, filter| !always_visible_filters.include?(name) }
22
31
  end
23
32
 
33
+ # rendering
34
+ def to_partial_path
35
+ 'azahara_schema/schema'
36
+ end
37
+
24
38
  end
25
39
  end
@@ -4,7 +4,11 @@ module AzaharaSchema
4
4
  attr_reader :schema
5
5
 
6
6
  def self.key
7
- self.name.split('::').last.underscore
7
+ self.name.split('::').last.sub(/Output$/, '').underscore
8
+ end
9
+
10
+ def key
11
+ self.class.key
8
12
  end
9
13
 
10
14
  def initialize(schema)
@@ -23,5 +27,10 @@ module AzaharaSchema
23
27
  model_name.i18n_key
24
28
  end
25
29
 
30
+ # rendering
31
+ def to_partial_path
32
+ 'azahara_schema/outputs/'+key
33
+ end
34
+
26
35
  end
27
36
  end
@@ -1,6 +1,8 @@
1
1
  module AzaharaSchema
2
2
  class Outputs
3
3
 
4
+ include Enumerable
5
+
4
6
  def self.registered_outputs
5
7
  @registered_outputs ||= {}
6
8
  end
@@ -26,5 +28,11 @@ module AzaharaSchema
26
28
  self.class.output_class(output).new(@schema)
27
29
  end
28
30
 
31
+ def each(&block)
32
+ @schema.enabled_outputs.each do |o|
33
+ yield output(o)
34
+ end
35
+ end
36
+
29
37
  end
30
38
  end
@@ -29,7 +29,7 @@ module AzaharaSchema
29
29
  operators_for_filters[filter] = operators
30
30
  end
31
31
 
32
- attr_accessor :model, :association, :parent_schema
32
+ attr_accessor :model, :enabled_outputs, :association, :parent_schema
33
33
  attr_accessor :search_query
34
34
 
35
35
  def initialize(model, **attributes)
@@ -37,6 +37,7 @@ module AzaharaSchema
37
37
  @association = attributes[:association]
38
38
  @parent_schema = attributes[:parent_schema]
39
39
  @column_names = attributes[:columns]
40
+ @enabled_outputs = attributes[:outputs] || default_outputs
40
41
  end
41
42
 
42
43
  def searchable_attributes
@@ -66,6 +67,10 @@ module AzaharaSchema
66
67
 
67
68
  # DEFAULTS
68
69
 
70
+ def default_outputs
71
+ []
72
+ end
73
+
69
74
  def default_columns
70
75
  [main_attribute_name]
71
76
  end
@@ -173,7 +178,9 @@ module AzaharaSchema
173
178
  end
174
179
 
175
180
  def attribute_for_column(col)
176
- Attribute.new(model, col.name, col.type)
181
+ t = 'list' if model.defined_enums[col.name]
182
+ t ||= col.type
183
+ Attribute.new(model, col.name, t)
177
184
  end
178
185
 
179
186
  def initialize_available_attributes
@@ -199,18 +206,15 @@ module AzaharaSchema
199
206
  query.split if query
200
207
  end
201
208
 
202
- def entities
203
- scope = model.respond_to?(:visible) ? model.visible : model.all
204
- columns.each do |col|
205
- scope = col.add_preload(scope)
206
- end
209
+ def entity_scope
210
+ model.respond_to?(:visible) ? model.visible : model.all
211
+ end
212
+
213
+ def filtered_scope
214
+ scope = entity_scope
207
215
  filters.each do |name, attrs|
208
216
  scope = available_filters[name].add_statement(scope, attrs[:o], attrs[:v])
209
217
  end
210
- sort.each do |name, order|
211
- att = attribute(name)
212
- scope = att.add_sort(scope, order) if att
213
- end
214
218
  if (tokens = tokenize_search_query)
215
219
  searchable_attributes.each{|a| scope = a.add_join(scope) }
216
220
  arl = searchable_attributes[0].arel_statement('~', tokens) if searchable_attributes.any?
@@ -220,6 +224,22 @@ module AzaharaSchema
220
224
  scope
221
225
  end
222
226
 
227
+ def entity_count
228
+ filtered_scope.count
229
+ end
230
+
231
+ def entities
232
+ scope = filtered_scope
233
+ columns.each do |col|
234
+ scope = col.add_preload(scope)
235
+ end
236
+ sort.each do |name, order|
237
+ att = attribute(name)
238
+ scope = att.add_sort(scope, order) if att
239
+ end
240
+ scope
241
+ end
242
+
223
243
  def build_json_options!(options={})
224
244
  columns.each{|col| col.build_json_options!(options) }
225
245
  options
@@ -1,3 +1,3 @@
1
1
  module AzaharaSchema
2
- VERSION = '0.1.12'
2
+ VERSION = '0.1.13'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: azahara_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ondřej Ezr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-15 00:00:00.000000000 Z
11
+ date: 2017-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -61,6 +61,7 @@ files:
61
61
  - app/models/action_schema/application_record.rb
62
62
  - app/views/azahara_schema/_filters.html.erb
63
63
  - app/views/azahara_schema/_index_form.html.erb
64
+ - app/views/azahara_schema/_schema.html.erb
64
65
  - app/views/layouts/action_schema/application.html.erb
65
66
  - config/locales/cs.yml
66
67
  - config/routes.rb
@@ -68,7 +69,10 @@ files:
68
69
  - lib/azahara_schema/aggregation_attribute.rb
69
70
  - lib/azahara_schema/association_attribute.rb
70
71
  - lib/azahara_schema/attribute.rb
72
+ - lib/azahara_schema/attribute_formatter.rb
71
73
  - lib/azahara_schema/attribute_name.rb
74
+ - lib/azahara_schema/cancan/controller_resource_patch.rb
75
+ - lib/azahara_schema/controller_additions.rb
72
76
  - lib/azahara_schema/derived_attribute.rb
73
77
  - lib/azahara_schema/engine.rb
74
78
  - lib/azahara_schema/field_format.rb
@@ -98,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
102
  version: '0'
99
103
  requirements: []
100
104
  rubyforge_project:
101
- rubygems_version: 2.7.2
105
+ rubygems_version: 2.7.3
102
106
  signing_key:
103
107
  specification_version: 4
104
108
  summary: Gem to support developement of rails application with schema over an entity