Class: Translatable::DatabaseStrategies::SQLite
- Defined in:
- lib/translatable/database_strategies/sqlite.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/sqlite.rb', line 4 def column_type :json end |
#migration_example(table_name, column_name) ⇒ Object
8 9 10 |
# File 'lib/translatable/database_strategies/sqlite.rb', line 8 def migration_example(table_name, column_name) "add_column :#{table_name}, :#{column_name}, :json, 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 |
# File 'lib/translatable/database_strategies/sqlite.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 = [] search_locales.each do |locale| attributes.each do |field, value| json_path = "$.#{locale}.#{field}" if case_sensitive conditions << "json_extract(#{column_name}, ?) = ?" bind_values += [json_path, value.to_s] else conditions << "json_extract(#{column_name}, ?) LIKE ? COLLATE NOCASE" bind_values += [json_path, value.to_s] end end end where_clause = conditions.join(' OR ') model_class.where(where_clause, *bind_values) end |