Module: Vocabulary
- Defined in:
- lib/vocabulary.rb
Class Method Summary collapse
-
.attributes(model, mode: nil) ⇒ Object
Concept is non-id, non-timestamp, non-hash attributes that are reasonably human-visualized.
-
.get_model(name) ⇒ Object
From a String.
-
.words(model: nil, attribute: nil, min: 0, max: nil, limit: nil, begins_with: nil, contains: nil, project_id: []) ⇒ Object
TODO: differentiate between no data and bad request.
Class Method Details
.attributes(model, mode: nil) ⇒ Object
Concept is non-id, non-timestamp, non-hash attributes that are reasonably human-visualized
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/vocabulary.rb', line 49 def self.attributes(model, mode: nil) return [] if model.nil? xml_fields = %i{svg_clip} # for now just subtract known unprocessable fields across models. a = ApplicationEnumeration.attributes(model.new) - xml_fields case mode&.to_sym when :editable a.delete_if{|v| v =~ /cached|md5|document_file|_type\Z|\Atype\Z|rank_class/} # needs to be in application enumeration probably a else a end end |
.get_model(name) ⇒ Object
From a String.
65 66 67 68 69 70 71 |
# File 'lib/vocabulary.rb', line 65 def self.get_model(name) begin klass = ::ApplicationController.new().whitelist_constantize(name) rescue KeyError nil end end |
.words(model: nil, attribute: nil, min: 0, max: nil, limit: nil, begins_with: nil, contains: nil, project_id: []) ⇒ Object
TODO: differentiate between no data and bad request
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/vocabulary.rb', line 5 def self.words(model: nil, attribute: nil, min: 0, max: nil, limit: nil, begins_with: nil, contains: nil, project_id: []) klass = get_model(model) return nil if klass.nil? t = klass.columns_hash[attribute]&.type # Happens on bad request return nil if t.nil? || [:xml].include?(t) if klass.new.attributes.symbolize_keys.keys.include?(:project_id) words = klass.where(project_id:) else words = klass end if [:string, :text].include?(t) c = "COUNT(\"#{attribute}\")" # double quote to handle things like `group` words = words.where("\"#{attribute}\" like '#{begins_with}%'") if begins_with.presence words = words.where("\"#{attribute}\" like '%#{contains}%'") if contains.presence words = words.having("#{c} > ?", min) if min.presence words = words.having("#{c} < ?", max) if max.presence words = words.limit(limit) if limit return words.group("\"#{attribute}\"").order(c + ' DESC').count else c = "COUNT(#{attribute})" words = words.where( "#{attribute}::text like '#{begins_with}%'") if begins_with.presence words = words.where("#{attribute}::text like '%#{contains}%'") if contains.presence words = words.having("#{c} > ?", min) if min.presence words = words.having("#{c} < ?", max) if max.presence words = words.limit(limit) if limit return words.group(attribute).order(c + ' DESC').count end end |