Class: AzaharaSchema::Schema
- Inherits:
-
Object
- Object
- AzaharaSchema::Schema
- Defined in:
- lib/azahara_schema/schema.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#association ⇒ Object
Returns the value of attribute association.
-
#default_scope ⇒ Object
Returns the value of attribute default_scope.
-
#enabled_outputs ⇒ Object
Returns the value of attribute enabled_outputs.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#model ⇒ Object
Returns the value of attribute model.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#parent_schema ⇒ Object
Returns the value of attribute parent_schema.
-
#search_query ⇒ Object
Returns the value of attribute search_query.
Class Method Summary collapse
- .attribute(model, name, attr_type = nil) ⇒ Object
- .attribute_for_column(model, col, attr_type = nil) ⇒ Object
- .attribute_type(model, name, col = nil) ⇒ Object
- .enabled_filters(*filter_names) ⇒ Object
- .filter_operators(filter, operators) ⇒ Object
- .operators_for_filters ⇒ Object
- .schema_class_for(klass) ⇒ Object
- .schema_for(klass, *attributes) ⇒ Object
Instance Method Summary collapse
- #add_filter(name, operator, values) ⇒ Object
-
#add_short_filter(name, str) ⇒ Object
ACCESSORS.
- #add_sort(name, order = :asc) ⇒ Object
- #as_json(options = {}) ⇒ Object
- #association_path ⇒ Object
- #attribute(name) ⇒ Object
- #attribute_for_column(col) ⇒ Object
- #available_associations ⇒ Object
- #available_attributes ⇒ Object
- #available_attributes_hash ⇒ Object
- #available_columns ⇒ Object
- #available_filters ⇒ Object
- #base_scope ⇒ Object
- #build_json_options!(options = {}) ⇒ Object
- #collapsable_filters ⇒ Object
- #column_names ⇒ Object
- #column_names=(values) ⇒ Object
- #columns ⇒ Object
- #default_columns ⇒ Object
-
#default_outputs ⇒ Object
DEFAULTS.
- #default_sort ⇒ Object
- #disabled_filters ⇒ Object
- #enabled_filter_names ⇒ Object
- #enabled_filters ⇒ Object
- #entities ⇒ Object
- #entity_as_json(entity, options = nil) ⇒ Object
- #entity_count ⇒ Object
- #entity_scope(scope = base_scope) ⇒ Object
- #filtered_scope ⇒ Object
- #filters ⇒ Object
-
#from_params(params) ⇒ Object
serialization.
- #include_associated_attributes? ⇒ Boolean
-
#initialize(model, **attributes) ⇒ Schema
constructor
A new instance of Schema.
- #initialize_available_attributes ⇒ Object
-
#main_attribute_name ⇒ Object
just a dummy implementation.
- #nested_relations_depth ⇒ Object
- #operator_for(fname) ⇒ Object
- #operators_for(filter_name) ⇒ Object
- #outputs ⇒ Object
- #searchable_attributes ⇒ Object
- #sort ⇒ Object
- #to_param ⇒ Object
- #tokenize_search_query(query = search_query) ⇒ Object
- #uncollapsable_filters ⇒ Object
- #user_available_filters ⇒ Object
- #value_for(fname) ⇒ Object
Constructor Details
#initialize(model, **attributes) ⇒ Schema
Returns a new instance of Schema.
70 71 72 73 74 75 76 77 |
# File 'lib/azahara_schema/schema.rb', line 70 def initialize(model, **attributes) @model = model @association = attributes[:association] @parent_schema = attributes[:parent_schema] @column_names = attributes[:columns] @enabled_outputs = attributes[:outputs] || default_outputs @nested_relations_depth = attributes[:relations_depth] end |
Instance Attribute Details
#association ⇒ Object
Returns the value of attribute association.
66 67 68 |
# File 'lib/azahara_schema/schema.rb', line 66 def association @association end |
#default_scope ⇒ Object
Returns the value of attribute default_scope.
67 68 69 |
# File 'lib/azahara_schema/schema.rb', line 67 def default_scope @default_scope end |
#enabled_outputs ⇒ Object
Returns the value of attribute enabled_outputs.
66 67 68 |
# File 'lib/azahara_schema/schema.rb', line 66 def enabled_outputs @enabled_outputs end |
#limit ⇒ Object
Returns the value of attribute limit.
68 69 70 |
# File 'lib/azahara_schema/schema.rb', line 68 def limit @limit end |
#model ⇒ Object
Returns the value of attribute model.
66 67 68 |
# File 'lib/azahara_schema/schema.rb', line 66 def model @model end |
#offset ⇒ Object
Returns the value of attribute offset.
68 69 70 |
# File 'lib/azahara_schema/schema.rb', line 68 def offset @offset end |
#parent_schema ⇒ Object
Returns the value of attribute parent_schema.
66 67 68 |
# File 'lib/azahara_schema/schema.rb', line 66 def parent_schema @parent_schema end |
#search_query ⇒ Object
Returns the value of attribute search_query.
67 68 69 |
# File 'lib/azahara_schema/schema.rb', line 67 def search_query @search_query end |
Class Method Details
.attribute(model, name, attr_type = nil) ⇒ Object
44 45 46 47 |
# File 'lib/azahara_schema/schema.rb', line 44 def self.attribute(model, name, attr_type=nil) attr_type ||= attribute_type(model, name) Attribute.new(model, name, attr_type) end |
.attribute_for_column(model, col, attr_type = nil) ⇒ Object
49 50 51 |
# File 'lib/azahara_schema/schema.rb', line 49 def self.attribute_for_column(model, col, attr_type=nil) attribute(model, col.name, attr_type || attribute_type(model, col.name, col) ) end |
.attribute_type(model, name, col = nil) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/azahara_schema/schema.rb', line 35 def self.attribute_type(model, name, col=nil) col ||= model.columns_hash[name.to_s] if model.defined_enums[col.name] 'list' else col && col.type end end |
.enabled_filters(*filter_names) ⇒ Object
53 54 55 56 |
# File 'lib/azahara_schema/schema.rb', line 53 def self.enabled_filters(*filter_names) @enabled_filters = filter_names if filter_names.any? @enabled_filters ||= [] end |
.filter_operators(filter, operators) ⇒ Object
62 63 64 |
# File 'lib/azahara_schema/schema.rb', line 62 def self.filter_operators(filter, operators) operators_for_filters[filter] = operators end |
.operators_for_filters ⇒ Object
58 59 60 |
# File 'lib/azahara_schema/schema.rb', line 58 def self.operators_for_filters @operators_for_filters ||= {} end |
.schema_class_for(klass) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/azahara_schema/schema.rb', line 6 def self.schema_class_for(klass) klasses = [klass] while klass != klass.base_class klass = klass.superclass klasses << klass end klasses.each do |kls| schema_klass = "#{kls.name}Schema".safe_constantize return schema_klass if schema_klass end AzaharaSchema::Schema end |
.schema_for(klass, *attributes) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/azahara_schema/schema.rb', line 19 def self.schema_for(klass, *attributes) model = klass klasses = [klass] while klass != klass.base_class klass = klass.superclass klasses << klass end klasses.each do |kls| schema_klass = "#{kls.name}Schema".safe_constantize if schema_klass return kls.try(:abstract_class?) ? schema_klass.new(model, *attributes) : schema_klass.new(*attributes) end end AzaharaSchema::Schema.new(model, *attributes) end |
Instance Method Details
#add_filter(name, operator, values) ⇒ Object
143 144 145 146 |
# File 'lib/azahara_schema/schema.rb', line 143 def add_filter(name, operator, values) raise 'filter ('+name+') is not defined!' unless available_filters.key?(name) filters[name] = { o: operator, v: values } end |
#add_short_filter(name, str) ⇒ Object
ACCESSORS
133 134 135 136 137 138 139 140 141 |
# File 'lib/azahara_schema/schema.rb', line 133 def add_short_filter(name, str) attrs = str.split('|') if attrs.size == 2 operator, values = attrs elsif attrs.size == 1 operator, values = '=', attrs.first end add_filter(name, operator, values.split('\\')) end |
#add_sort(name, order = :asc) ⇒ Object
148 149 150 |
# File 'lib/azahara_schema/schema.rb', line 148 def add_sort(name, order=:asc) sort[name] = order end |
#as_json(options = {}) ⇒ Object
318 319 320 321 |
# File 'lib/azahara_schema/schema.rb', line 318 def as_json(={}) () entities.collect{|entity| entity_as_json(entity, ) } end |
#association_path ⇒ Object
209 210 211 |
# File 'lib/azahara_schema/schema.rb', line 209 def association_path @association_path ||= parent_schema ? ( parent_schema.association_path + [association.name] ) : [model.model_name.element.to_sym] end |
#attribute(name) ⇒ Object
166 167 168 |
# File 'lib/azahara_schema/schema.rb', line 166 def attribute(name) available_attributes.detect{|att| att.name == name} end |
#attribute_for_column(col) ⇒ Object
225 226 227 |
# File 'lib/azahara_schema/schema.rb', line 225 def attribute_for_column(col) self.class.attribute_for_column(model, col) end |
#available_associations ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/azahara_schema/schema.rb', line 213 def available_associations return [] unless include_associated_attributes? @available_associations ||= model.reflect_on_all_associations.select do |association| !association.[:polymorphic] && association.klass != model && !association_path.include?( association.name.to_s.singularize.to_sym ) && !association_path.include?( association.name.to_s.pluralize.to_sym ) end.collect do |association| AzaharaSchema::Schema.schema_for(association.klass, parent_schema: self, association: association, relations_depth: nested_relations_depth.try(:-, 1)) end end |
#available_attributes ⇒ Object
170 171 172 173 174 175 176 |
# File 'lib/azahara_schema/schema.rb', line 170 def available_attributes unless @available_attributes initialize_available_attributes @available_attributes.each{|at| at.table_alias = Array(association_path[1..-1]).collect(&:to_s).join('_').presence } end @available_attributes end |
#available_attributes_hash ⇒ Object
178 179 180 |
# File 'lib/azahara_schema/schema.rb', line 178 def available_attributes_hash available_attributes.inject({}){|obj, aa| obj[aa.name] = aa; obj } end |
#available_columns ⇒ Object
182 183 184 |
# File 'lib/azahara_schema/schema.rb', line 182 def available_columns @available_columns ||= available_attributes.select{|att| att.column? } end |
#available_filters ⇒ Object
201 202 203 |
# File 'lib/azahara_schema/schema.rb', line 201 def available_filters @available_filters ||= available_attributes_hash end |
#base_scope ⇒ Object
252 253 254 |
# File 'lib/azahara_schema/schema.rb', line 252 def base_scope model.all end |
#build_json_options!(options = {}) ⇒ Object
294 295 296 297 |
# File 'lib/azahara_schema/schema.rb', line 294 def (={}) columns.each{|col| col.() } end |
#collapsable_filters ⇒ Object
370 371 372 |
# File 'lib/azahara_schema/schema.rb', line 370 def collapsable_filters available_filters end |
#column_names ⇒ Object
88 89 90 |
# File 'lib/azahara_schema/schema.rb', line 88 def column_names @column_names ||= default_columns end |
#column_names=(values) ⇒ Object
83 84 85 86 |
# File 'lib/azahara_schema/schema.rb', line 83 def column_names=(values) @column_names = values @columns = nil end |
#columns ⇒ Object
92 93 94 |
# File 'lib/azahara_schema/schema.rb', line 92 def columns @columns ||= available_attributes_hash.slice(*column_names).values end |
#default_columns ⇒ Object
110 111 112 |
# File 'lib/azahara_schema/schema.rb', line 110 def default_columns [main_attribute_name] end |
#default_outputs ⇒ Object
DEFAULTS
106 107 108 |
# File 'lib/azahara_schema/schema.rb', line 106 def default_outputs [AzaharaSchema::Outputs.registered_outputs.keys.first].compact end |
#default_sort ⇒ Object
114 115 116 |
# File 'lib/azahara_schema/schema.rb', line 114 def default_sort {} end |
#disabled_filters ⇒ Object
190 191 192 |
# File 'lib/azahara_schema/schema.rb', line 190 def disabled_filters [] end |
#enabled_filter_names ⇒ Object
194 195 196 197 198 199 |
# File 'lib/azahara_schema/schema.rb', line 194 def enabled_filter_names names = self.class.enabled_filters if self.class.enabled_filters.any? names ||= available_attributes_hash.keys names &= enabled_filters if enabled_filters names -= disabled_filters end |
#enabled_filters ⇒ Object
186 187 188 |
# File 'lib/azahara_schema/schema.rb', line 186 def enabled_filters end |
#entities ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/azahara_schema/schema.rb', line 280 def entities scope = filtered_scope columns.each do |col| scope = col.add_preload(scope) end sort.each do |name, order| att = attribute(name) scope = att.add_sort(scope, order) if att end scope = scope.offset(offset) if offset scope = scope.limit(limit) if limit scope end |
#entity_as_json(entity, options = nil) ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/azahara_schema/schema.rb', line 299 def entity_as_json(entity, =nil) attr_hash = entity.as_json() # TODO serializable_add_includes(options) do |association, records, opts| columns.each do |col| col_sub_hash = attr_hash sub_col = col while sub_col.is_a?(AzaharaSchema::AssociationAttribute) col_sub_hash = (col_sub_hash[sub_col.association.name.to_s] ||= {}) sub_col = sub_col.attribute end if col.type == 'love' col_sub_hash[sub_col.name] = sub_col.available_values.detect{|l, v| v == col_sub_hash[sub_col.name] }.try(:[], 0) else col_sub_hash[sub_col.name] = col.value(entity) end end attr_hash end |
#entity_count ⇒ Object
276 277 278 |
# File 'lib/azahara_schema/schema.rb', line 276 def entity_count filtered_scope.count end |
#entity_scope(scope = base_scope) ⇒ Object
256 257 258 259 260 |
# File 'lib/azahara_schema/schema.rb', line 256 def entity_scope(scope=base_scope) scope = scope.visible if scope.respond_to?(:visible) scope = scope.send(self.default_scope) if self.default_scope scope end |
#filtered_scope ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/azahara_schema/schema.rb', line 262 def filtered_scope scope = entity_scope filters.each do |name, attrs| scope = available_filters[name].add_statement(scope, attrs[:o], attrs[:v]) end if (tokens = tokenize_search_query) searchable_attributes.each{|a| scope = a.add_join(scope) } arl = searchable_attributes[0].arel_statement('~', tokens) if searchable_attributes.any? Array(searchable_attributes[1..-1]).each{|att| arl = arl.or( att.arel_statement('~', tokens) ) } scope = scope.where(arl) end scope end |
#filters ⇒ Object
96 97 98 |
# File 'lib/azahara_schema/schema.rb', line 96 def filters @filters ||= {} end |
#from_params(params) ⇒ Object
serialization
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/azahara_schema/schema.rb', line 325 def from_params(params) if params[:f] filter_params = params[:f].permit(available_filters.keys + [available_filters.keys.inject({}){|o,name| o[name] = []; o }]).to_h filter_params.each do |name, filter_value| next if filter_value.blank? if filter_value.is_a?(Array) add_filter(name, '=', filter_value) else add_short_filter(name, filter_value) end end end if params[:c].is_a?(Array) self.column_names = params[:c].to_a end if params[:sort] @sort = nil params[:sort].each do |k, sort| add_sort(sort[:path], sort['desc'] == 'true' ? :desc : :asc ) end end self.default_scope = params[:default_scope] if params[:default_scope] self.search_query = params[:q] unless params[:q].blank? self.offset = params[:offset] if params[:offset] self.limit = params[:limit] if params[:limit] end |
#include_associated_attributes? ⇒ Boolean
127 128 129 |
# File 'lib/azahara_schema/schema.rb', line 127 def include_associated_attributes? nested_relations_depth > 0 end |
#initialize_available_attributes ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/azahara_schema/schema.rb', line 229 def initialize_available_attributes @available_attributes ||= [] model.columns.each do |col| @available_attributes << attribute_for_column(col) end available_associations.each do |asoc_schema| asoc_schema.available_attributes.each do |asoc_attribute| next if asoc_attribute.is_a?(AggregationAttribute) added_attribute = AssociationAttribute.new(model, asoc_schema, asoc_attribute) @available_attributes << added_attribute @available_attributes << AggregationAttribute.new(model, added_attribute) if asoc_attribute.aggregable? end end end |
#main_attribute_name ⇒ Object
just a dummy implementation
119 120 121 |
# File 'lib/azahara_schema/schema.rb', line 119 def main_attribute_name available_attributes.detect{|att| att.name != 'id' }.name end |
#nested_relations_depth ⇒ Object
123 124 125 |
# File 'lib/azahara_schema/schema.rb', line 123 def nested_relations_depth @nested_relations_depth || 4 end |
#operator_for(fname) ⇒ Object
152 153 154 |
# File 'lib/azahara_schema/schema.rb', line 152 def operator_for(fname) filters[fname] && filters[fname][:o] end |
#operators_for(filter_name) ⇒ Object
160 161 162 163 164 |
# File 'lib/azahara_schema/schema.rb', line 160 def operators_for(filter_name) operators = available_filters[filter_name] && available_filters[filter_name].available_operators operators &= self.class.operators_for_filters[filter_name] if operators && self.class.operators_for_filters[filter_name] operators end |
#outputs ⇒ Object
244 245 246 |
# File 'lib/azahara_schema/schema.rb', line 244 def outputs Outputs.new(self) end |
#searchable_attributes ⇒ Object
79 80 81 |
# File 'lib/azahara_schema/schema.rb', line 79 def searchable_attributes @searchable_attributes ||= available_attributes.select{|a| a.searchable? } end |
#sort ⇒ Object
100 101 102 |
# File 'lib/azahara_schema/schema.rb', line 100 def sort @sort ||= default_sort end |
#to_param ⇒ Object
352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/azahara_schema/schema.rb', line 352 def to_param params = {} params[:f] = {} filters.each do |fname, attrs| params[:f][fname] = "#{attrs[:o]}|#{Array(attrs[:v]).collect{|v| v.to_s}.join('\\')}" end params[:default_scope] = default_scope if default_scope params[:c] = column_names params[:q] = search_query if params[:q] params[:offset] = offset params[:limit] = limit params end |
#tokenize_search_query(query = search_query) ⇒ Object
248 249 250 |
# File 'lib/azahara_schema/schema.rb', line 248 def tokenize_search_query(query=search_query) query.split if query end |
#uncollapsable_filters ⇒ Object
366 367 368 |
# File 'lib/azahara_schema/schema.rb', line 366 def uncollapsable_filters {} end |
#user_available_filters ⇒ Object
205 206 207 |
# File 'lib/azahara_schema/schema.rb', line 205 def user_available_filters available_filters.slice(*enabled_filter_names) end |
#value_for(fname) ⇒ Object
156 157 158 |
# File 'lib/azahara_schema/schema.rb', line 156 def value_for(fname) filters[fname] && filters[fname][:v] end |