azahara_schema 0.2.7 → 0.3.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
  SHA256:
3
- metadata.gz: 855ce45cd183a323fc4b01598cc8ed54b2440284cbe2e888baaf4940d8566d19
4
- data.tar.gz: 9450e8a3f0d8d95baf2166f5ff0afe54e0558b71d9b49a91c6c8e09b01c3dc83
3
+ metadata.gz: 5524a5493e19e41523f7e1bc68d332d60f8632b135eb332f2d0c647e7b078123
4
+ data.tar.gz: f6b312c7ce2da1b2eb24d4d0fda8c1f197816862014444f97e6285936c546555
5
5
  SHA512:
6
- metadata.gz: 115e3da2d1d6023c94f3a6052b7c4c5e817e7ffd81579a8ecbba6937d478731faa0dfa4bbb045d388293173d6d489945ab3eb2a74fdac0fa518ca7ca3494271a
7
- data.tar.gz: 3084ce3dab7bd6d5f5845d3e5997ab93996af302ca16bd3764f196f23dc7ebd563d856b7f8f2f6a966ba4a03a634cf03b330e6214a17e53ce5611d0975489fac
6
+ metadata.gz: c5f2e34b86aa21762d72af69f1a214af3b8418aee16dfa224cbda52b3cb1b74d6b24f216286f1384769a1825e3e9904ccbe7662c92e23e9ed1824d01ec66630a
7
+ data.tar.gz: 2339248d1a7a6cfce3b92ca2e6ca8bfd214ae884a037cbb342b29135fb062b0cfdecd7a5d8b20c5d0317d5ba0b0cc263bbd27da57c1bfc5e462e1e578cac7e34
@@ -40,8 +40,12 @@ module AzaharaSchema
40
40
  end
41
41
  end
42
42
 
43
- # translates values to list_values
44
- # TODO: not needed to do it for every value - for example districts are not translatable
43
+ def azahara_export_path(schema, export_format)
44
+ polymorphic_path(schema.model, schema.to_param.merge(format: export_format))
45
+ rescue NoMethodError => err
46
+ nil
47
+ end
48
+
45
49
  def list_values_for_select(attribute)
46
50
  if attribute.format.format_name == 'list'
47
51
  attribute.available_values.collect do |l, val|
@@ -52,6 +56,10 @@ module AzaharaSchema
52
56
  end
53
57
  end
54
58
 
59
+ def azahara_attribute_for(model, name)
60
+ AzaharaSchema::Schema.schema_class_for(model).attribute(model, name)
61
+ end
62
+
55
63
  def attribute_formatter_for(schema_or_model, **options)
56
64
  AttributeFormatter.formatter_for(schema_or_model).new(schema_or_model, self, options)
57
65
  end
@@ -5,8 +5,10 @@
5
5
  <%= render output %>
6
6
  <% end %>
7
7
  <div class="exports text-right">
8
- <%= link_to polymorphic_path(schema.model, schema.to_param.merge(format: :csv)) do %>
9
- <i class="fa fa-file-o"></i>
10
- <%= t('label_export_as', as: 'CSV') %>
8
+ <% if (csv_path = azahara_export_path(schema, :csv)) %>
9
+ <%= link_to csv_path do %>
10
+ <i class="fa fa-file-o"></i>
11
+ <%= t('label_export_as', as: 'CSV') %>
12
+ <% end %>
11
13
  <% end %>
12
14
  </div>
@@ -11,6 +11,8 @@ module AzaharaSchema
11
11
  I18n.t(i18n_scoped_key, options.merge(default: i18n_fallback_keys))
12
12
  end
13
13
 
14
+ # translates values to list_values
15
+ # TODO: not needed to do it for every value - for example districts are not translatable
14
16
  def human_list_value(value, **options)
15
17
  return '' unless value.present?
16
18
  I18n.t(i18n_scoped_list_key(value.to_s), options.merge(default: i18n_list_fallback_keys(value)+[value.to_s]))
@@ -2,9 +2,7 @@ module AzaharaSchema
2
2
  module ControllerAdditions
3
3
 
4
4
  def azahara_schema_index(**options, &block)
5
- @resource_schema = self.class.cancan_resource_class.new(self).send(:collection_instance)
6
- @resource_schema.from_params(params.reverse_merge(options[:defaults] || {}))
7
- yield(@resource_schema) if block_given?
5
+ azahara_schema_for_index(options, &block)
8
6
  respond_to do |format|
9
7
  format.html
10
8
  format.json {
@@ -29,5 +27,27 @@ module AzaharaSchema
29
27
  end
30
28
  end
31
29
 
30
+ def azahara_schema_api_index(**options, &block)
31
+ azahara_schema_for_index(options, &block)
32
+ respond_to do |format|
33
+ format.json {
34
+ json_result = {}
35
+ if params['_type'] == 'count'
36
+ json_result = {count: @resource_schema.entity_count}
37
+ else
38
+ json_result = {entities: @resource_schema, count: @resource_schema.entity_count}
39
+ end
40
+ render json: json_result
41
+ }
42
+ end
43
+ end
44
+
45
+ private
46
+ def azahara_schema_for_index(**options, &block)
47
+ @resource_schema = self.class.cancan_resource_class.new(self).send(:collection_instance)
48
+ @resource_schema.from_params(params.reverse_merge(options[:defaults] || {}))
49
+ yield(@resource_schema) if block_given?
50
+ end
51
+
32
52
  end
33
53
  end
@@ -1,8 +1,9 @@
1
1
  module AzaharaSchema
2
2
  class ModelSchema < Schema
3
3
 
4
- def initialize(**attrs)
5
- super(model, attrs)
4
+ def initialize(*attrs)
5
+ attributes = attrs.last.is_a?(Hash) ? attrs.pop : {}
6
+ super(attrs.first || model, attributes)
6
7
  end
7
8
 
8
9
  def export_template
@@ -3,7 +3,21 @@ require 'active_support' #Hash.slice
3
3
  module AzaharaSchema
4
4
  class Schema
5
5
 
6
+ def self.schema_class_for(klass)
7
+ klasses = [klass]
8
+ while klass != klass.base_class
9
+ klass = klass.superclass
10
+ klasses << klass
11
+ end
12
+ klasses.each do |kls|
13
+ schema_klass = "#{kls.name}Schema".safe_constantize
14
+ return schema_klass if schema_klass
15
+ end
16
+ AzaharaSchema::Schema
17
+ end
18
+
6
19
  def self.schema_for(klass, *attributes)
20
+ model = klass
7
21
  klasses = [klass]
8
22
  while klass != klass.base_class
9
23
  klass = klass.superclass
@@ -11,9 +25,27 @@ module AzaharaSchema
11
25
  end
12
26
  klasses.each do |kls|
13
27
  schema_klass = "#{kls.name}Schema".safe_constantize
14
- return schema_klass.new(*attributes) if schema_klass
28
+ if schema_klass
29
+ return kls.try(:abstract_class?) ? schema_klass.new(model, *attributes) : schema_klass.new(*attributes)
30
+ end
15
31
  end
16
- AzaharaSchema::Schema.new(klass, *attributes)
32
+ AzaharaSchema::Schema.new(model, *attributes)
33
+ end
34
+
35
+ def self.attribute_type(model, name)
36
+ col = model.columns_hash[name.to_s]
37
+ col && col.type
38
+ end
39
+
40
+ def self.attribute(model, name, attr_type=nil)
41
+ attr_type ||= attribute_type(model, name)
42
+ Attribute.new(model, name, attr_type)
43
+ end
44
+
45
+ def self.attribute_for_column(model, col, attr_type=nil)
46
+ attr_type ||= ('list' if model.defined_enums[col.name])
47
+ attr_type ||= col.type
48
+ attribute(model, col.name, attr_type)
17
49
  end
18
50
 
19
51
  def self.enabled_filters(*filter_names)
@@ -179,9 +211,7 @@ module AzaharaSchema
179
211
  end
180
212
 
181
213
  def attribute_for_column(col)
182
- t = 'list' if model.defined_enums[col.name]
183
- t ||= col.type
184
- Attribute.new(model, col.name, t)
214
+ self.class.attribute_for_column(model, col)
185
215
  end
186
216
 
187
217
  def initialize_available_attributes
@@ -1,3 +1,3 @@
1
1
  module AzaharaSchema
2
- VERSION = '0.2.7'
2
+ VERSION = '0.3.0'
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.2.7
4
+ version: 0.3.0
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: 2018-02-13 00:00:00.000000000 Z
11
+ date: 2018-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -103,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  version: '0'
104
104
  requirements: []
105
105
  rubyforge_project:
106
- rubygems_version: 2.7.5
106
+ rubygems_version: 2.7.6
107
107
  signing_key:
108
108
  specification_version: 4
109
109
  summary: Gem to support developement of rails application with schema over an entity