azahara_schema 0.1.2 → 0.1.3

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
  SHA1:
3
- metadata.gz: 97da3d5692c281b23103b2f29bb7d000e9a9a47d
4
- data.tar.gz: 6650308c13f3b416394d6899067768300f6a022a
3
+ metadata.gz: d672ff9453d94ce1d4926ca08e78bd3df9298fb3
4
+ data.tar.gz: b3101131a1b1d578edeaac3444af869212afb5e3
5
5
  SHA512:
6
- metadata.gz: b5a10fdff991b788f56fe2ffe2f3c72f65d3f8feea779dc5766706a31bc69130cbc2154014b78cd0088713400ae09f2d81b72d2130066f468ddc4432716a57e6
7
- data.tar.gz: 7a1f06279dfeed7351b9d10bbaeb9b5c3f0196c3d2fa6a26f29a4e1951599b56744b433e8e74266c81bd5f30b0e37bee7a06a1bac402b322fdbcab04e6b85247
6
+ metadata.gz: 1b3a6f4232a219e888beaf3f36b5b6e243ec286e4af28ce850afa6d2ad93a4468ed4e4cbbff562bce546544a8a6da6bf95513eb8a71491acd894681f504ff9b7
7
+ data.tar.gz: a99067d5f78de03f6672b2c8c33aff2695aa4ab6828f4ce85479f7fa7bd51505b29c271c5cd4dc0fea2efbb5b2f81895e43ed227bfef05ed2f9e4693a420a242
@@ -5,7 +5,9 @@ class Filter
5
5
  that = this
6
6
  @hiddenEl = $('<input>', {name: @valueEl.attr('name'), type: 'hidden'}).insertAfter(@valueEl)
7
7
  chngFnc = (evt)->
8
- that.hiddenEl.val(that.shortValue())
8
+ short_val = that.shortValue()
9
+ that.hiddenEl.val(short_val)
10
+ that.hiddenEl.prop('disabled', short_val == '')
9
11
 
10
12
  @operatorEl.change(chngFnc)
11
13
  @valueEl.change(chngFnc)
@@ -28,3 +30,6 @@ $.widget 'azahara_schema.filters',
28
30
  that.filters.push new Filter($(this).data('name'), $(this).find('.operator-field'), $(this).find('.value-field'))
29
31
 
30
32
  @form = @element.closest('form')
33
+ @form.on 'submit', ()->
34
+ that.form.find('.operator-field').prop('disabled', true)
35
+ that.form.find('.value-field').prop('disabled', true)
@@ -2,7 +2,7 @@
2
2
  <div id="filters_uncollapsable">
3
3
  <% schema.uncollapsable_filters.each do |name, filter| %>
4
4
  <div class="form-group row filter" data-name="<%= name %>">
5
- <div class="col-md-2 control-label"><%= label_tag "f[#{name}]", t('activerecord.attributes.'+schema.model.name.underscore+'.'+name, default: name.humanize) %></div>
5
+ <div class="col-md-2 control-label"><%= label_tag "f[#{name}]", filter.attribute_name.human %></div>
6
6
  <% if schema.operators_for(name).count > 1 %>
7
7
  <div class="col-md-2"><%= select_tag "f[#{name}]", options_for_select(operators_for_select(schema, name), schema.operator_for(name)), class: 'form-control operator-field' %></div>
8
8
  <div class="col-md-8">
@@ -16,10 +16,10 @@ module AzaharaSchema
16
16
 
17
17
  delegate :association, to: :schema
18
18
 
19
- def initialize(association_schema, attribute)
19
+ def initialize(model, association_schema, attribute)
20
20
  @schema = association_schema
21
21
  @attribute = attribute
22
- super(association.klass, association.name.to_s+'-'+attribute.name, attribute.type)
22
+ super(model, association.name.to_s+'-'+attribute.name, attribute.type)
23
23
  end
24
24
 
25
25
  def available_values
@@ -39,25 +39,26 @@ module AzaharaSchema
39
39
  end
40
40
 
41
41
  def value(parent)
42
- parent.public_send(association.name).to_s
42
+ if association.macro == :has_many
43
+ parent.public_send(association.name).collect{|record| attribute.value( record )}.flatten
44
+ else
45
+ attribute.value( parent.public_send(association.name) )
46
+ end
47
+ end
48
+
49
+ def association_hash
50
+ { association.name => attribute.association_hash }
43
51
  end
44
52
 
45
53
  def add_join(scope)
46
- if attribute.is_a?(AzaharaSchema::AssociationAttribute)
47
- scope.includes(association.name => attribute.association.name).references(association.name => attribute.association.name)
48
- else
49
- scope.includes(association.name).references(association.name)
50
- end
54
+ scope.includes(association_hash).references(association_hash)
51
55
  end
52
56
 
53
57
  def add_preload(scope)
54
- if attribute.is_a?(AzaharaSchema::AssociationAttribute)
55
- scope.preload(association.name => attribute.association.name)
56
- else
57
- scope.preload(association.name)
58
- end
58
+ scope.preload(association_hash)
59
59
  end
60
60
 
61
+ # TODO: heuristic for when add left outer join and when add inner join
61
62
  def add_statement(scope, operator, values)
62
63
  super(add_join(scope), operator, values)
63
64
  end
@@ -79,5 +79,13 @@ module AzaharaSchema
79
79
  options
80
80
  end
81
81
 
82
+ def attribute_name
83
+ AzaharaSchema::AttributeName.new(self)
84
+ end
85
+
86
+ def association_hash
87
+ {}
88
+ end
89
+
82
90
  end
83
91
  end
@@ -0,0 +1,37 @@
1
+ module AzaharaSchema
2
+ class AttributeName
3
+
4
+ attr_reader :attribute
5
+
6
+ def initialize(attribute)
7
+ @attribute = attribute
8
+ end
9
+
10
+ def human
11
+ I18n.t(i18n_scoped_key, default: i18n_fallback_keys)
12
+ end
13
+
14
+ def model_i18n_key
15
+ attribute.model.model_name.i18n_key
16
+ end
17
+
18
+ def i18n_key
19
+ attribute.name
20
+ end
21
+
22
+ def i18n_scoped_key
23
+ ('activerecord.attributes.' + model_i18n_key.to_s + '.' + i18n_key.to_s).to_sym
24
+ end
25
+
26
+ def i18n_fallback_keys
27
+ if attribute.respond_to?(:attribute)
28
+ parent_attr_name = attribute.attribute.attribute_name
29
+ keys = [ parent_attr_name.i18n_scoped_key.to_sym ]
30
+ keys.concat( parent_attr_name.i18n_fallback_keys )
31
+ keys
32
+ else
33
+ [ i18n_scoped_key.to_sym ]
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,6 +1,7 @@
1
1
  require 'azahara_schema/field_format'
2
2
  require 'azahara_schema/attribute'
3
3
  require 'azahara_schema/association_attribute'
4
+ require 'azahara_schema/attribute_name'
4
5
  require 'azahara_schema/outputs'
5
6
  require 'azahara_schema/output'
6
7
  require 'azahara_schema/schema'
@@ -29,11 +29,12 @@ module AzaharaSchema
29
29
  operators_for_filters[filter] = operators
30
30
  end
31
31
 
32
- attr_accessor :model, :association
32
+ attr_accessor :model, :association, :parent_schema
33
33
 
34
34
  def initialize(model, **attributes)
35
35
  @model = model
36
36
  @association = attributes[:association]
37
+ @parent_schema = attributes[:parent_schema]
37
38
  @column_names = attributes[:columns]
38
39
  end
39
40
 
@@ -55,7 +56,7 @@ module AzaharaSchema
55
56
  end
56
57
 
57
58
  def sort
58
- @sort ||= {}
59
+ @sort ||= default_sort
59
60
  end
60
61
 
61
62
  # DEFAULTS
@@ -64,11 +65,19 @@ module AzaharaSchema
64
65
  [main_attribute_name]
65
66
  end
66
67
 
68
+ def default_sort
69
+ {}
70
+ end
71
+
67
72
  # just a dummy implementation
68
73
  def main_attribute_name
69
74
  available_attributes.detect{|att| att.name != 'id' }.name
70
75
  end
71
76
 
77
+ def follow_nested_relations
78
+ true
79
+ end
80
+
72
81
  # ACCESSORS
73
82
 
74
83
  def add_short_filter(name, str)
@@ -141,10 +150,15 @@ module AzaharaSchema
141
150
  @available_filters ||= available_attributes_hash.slice(*enabled_filter_names)
142
151
  end
143
152
 
153
+ def association_path
154
+ @association_path ||= parent_schema ? ( [association.name].concat(parent_schema.association_path) ) : []
155
+ end
156
+
144
157
  def available_associations
145
- return [] if @association #only first level of association - would need to solve circular dependency first to add next level
146
- @available_associations ||= model.reflect_on_all_associations.collect do |association|
147
- AzaharaSchema::Schema.schema_for(association.klass, association: association)
158
+ @available_associations ||= model.reflect_on_all_associations.select do |association|
159
+ association.klass != model && !association_path.include?( association.name.to_s.singularize.to_sym ) && !association_path.include?( association.name.to_s.pluralize.to_sym )
160
+ end.collect do |association|
161
+ AzaharaSchema::Schema.schema_for(association.klass, parent_schema: self, association: association)
148
162
  end
149
163
  end
150
164
 
@@ -159,7 +173,7 @@ module AzaharaSchema
159
173
  end
160
174
  available_associations.each do |asoc_schema|
161
175
  asoc_schema.available_attributes.each do |asoc_attribute|
162
- @available_attributes << AssociationAttribute.new(asoc_schema, asoc_attribute)
176
+ @available_attributes << AssociationAttribute.new(model, asoc_schema, asoc_attribute)
163
177
  end
164
178
  end
165
179
  end
@@ -1,3 +1,3 @@
1
1
  module AzaharaSchema
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
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.2
4
+ version: 0.1.3
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-10-19 00:00:00.000000000 Z
11
+ date: 2017-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -67,6 +67,7 @@ files:
67
67
  - lib/azahara_schema.rb
68
68
  - lib/azahara_schema/association_attribute.rb
69
69
  - lib/azahara_schema/attribute.rb
70
+ - lib/azahara_schema/attribute_name.rb
70
71
  - lib/azahara_schema/engine.rb
71
72
  - lib/azahara_schema/field_format.rb
72
73
  - lib/azahara_schema/model_schema.rb