Class: Translatable::DatabaseStrategies::PostgreSQL
- Defined in:
- lib/translatable/database_strategies/postgresql.rb
Instance Method Summary collapse
- #column_type ⇒ Object
- #migration_example(table_name, column_name) ⇒ Object
- #where_translations_scope(model_class, attributes, locales: [], case_sensitive: false) ⇒ Object
Methods inherited from Base
Instance Method Details
#column_type ⇒ Object
4 5 6 |
# File 'lib/translatable/database_strategies/postgresql.rb', line 4 def column_type :jsonb end |
#migration_example(table_name, column_name) ⇒ Object
8 9 10 |
# File 'lib/translatable/database_strategies/postgresql.rb', line 8 def migration_example(table_name, column_name) "add_column :#{table_name}, :#{column_name}, :jsonb, default: {}, null: false" end |
#where_translations_scope(model_class, attributes, locales: [], case_sensitive: false) ⇒ Object
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 |
# File 'lib/translatable/database_strategies/postgresql.rb', line 12 def where_translations_scope(model_class, attributes, locales: [], case_sensitive: false) column_name = model_class.translations_column_name search_locales = locales.present? ? locales.map(&:to_s) : model_class.translatable_locales.map(&:to_s) return model_class.none if attributes.blank? conditions = [] bind_values = [] if case_sensitive search_locales.each do |locale| locale_attributes = { locale => attributes } conditions << "#{column_name} @> ?" bind_values << locale_attributes.to_json end else search_locales.each do |locale| attributes.each do |field, value| conditions << "(#{column_name} -> ? ->> ?) ILIKE ?" bind_values += [locale, field.to_s, value.to_s] end end end where_clause = conditions.join(' OR ') model_class.where(where_clause, *bind_values) end |