class Message

Included Modules

Extended Modules

Defined in:

models/message.cr

Constant Summary

COLUMNS = {"id" => {type: Int64, primary: true, converter: "Int64", db_column_name: "id", crystal_variable_name: id, presence: false, mass_assign: true}, "telegram_message_id" => {type: Int64, primary: false, converter: "Int64", db_column_name: "telegram_message_id", crystal_variable_name: telegram_message_id, presence: true, mass_assign: true}, "telegram_message_date" => {type: Int64, primary: false, converter: "Int64", db_column_name: "telegram_message_date", crystal_variable_name: telegram_message_date, presence: true, mass_assign: true}, "telegram_chat_id" => {type: Int64, primary: false, converter: "Int64", db_column_name: "telegram_chat_id", crystal_variable_name: telegram_chat_id, presence: true, mass_assign: true}, "telegram_chat_type" => {type: String, primary: false, converter: "String", db_column_name: "telegram_chat_type", crystal_variable_name: telegram_chat_type, presence: true, mass_assign: true}, "text" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "text", crystal_variable_name: text, presence: true, mass_assign: true}, "location" => {type: PG::Geo::Point | ::Nil, primary: false, converter: "PG::Geo::Point", db_column_name: "location", crystal_variable_name: location, presence: true, mass_assign: true}, "updated_at" => {type: Time, primary: false, converter: "Time", db_column_name: "updated_at", crystal_variable_name: updated_at, presence: true, mass_assign: true}, "created_at" => {type: Time, primary: false, converter: "Time", db_column_name: "created_at", crystal_variable_name: created_at, presence: true, mass_assign: true}, "user_id" => {type: Int64, primary: false, converter: "Int64", db_column_name: "user_id", crystal_variable_name: user_id, presence: false, mass_assign: true}} of Nil => Nil
POLYMORPHISM_SETTINGS = {} of Nil => Nil

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.build(x : NamedTuple) : self #

Build a new empty model and fill the columns using the NamedTuple in argument.

Returns the new model


[View source]
def self.build(x : NamedTuple, &block : self -> Nil) : self #

Build a new empty model and fill the columns using the NamedTuple in argument.

Returns the new model


[View source]
def self.create(x : NamedTuple, &block : self -> Nil) : self #

Build and new model and save it. Returns the model.

The model may not be saved due to validation failure; check the returned model errors? and persisted? flags.


[View source]
def self.create(**tuple, &block : self -> Nil) : self #

Build and new model and save it. Returns the model.

The model may not be saved due to validation failure; check the returned model errors? and persisted? flags.


[View source]
def self.create(x : NamedTuple) : self #

Build and new model and save it. Returns the model.

The model may not be saved due to validation failure; check the returned model errors? and persisted? flags.


[View source]
def self.create(**tuple) : self #

Build and new model and save it. Returns the model.

The model may not be saved due to validation failure; check the returned model errors? and persisted? flags.


[View source]
def self.create!(x : NamedTuple, &block : self -> Nil) : self #

Build and new model and save it. Returns the model.

Returns the newly inserted model Raises an exception if validation failed during the saving process.


[View source]
def self.create!(**tuple, &block : self -> Nil) : self #

Build and new model and save it. Returns the model.

Returns the newly inserted model Raises an exception if validation failed during the saving process.


[View source]
def self.create!(x : NamedTuple) : self #

Build and new model and save it. Returns the model.

Returns the newly inserted model Raises an exception if validation failed during the saving process.


[View source]
def self.create!(**tuple) : self #

Build and new model and save it. Returns the model.

Returns the newly inserted model Raises an exception if validation failed during the saving process.


[View source]
def self.new(h : Hash(String, _), cache : Lustra::Model::QueryCache | Nil = nil, persisted = false, fetch_columns = false) #

[View source]
def self.new(json : JSON::Any, cache : Lustra::Model::QueryCache | Nil = nil, persisted = false) #

[View source]
def self.new(t : NamedTuple, persisted = false) #

[View source]
def self.new #

[View source]

Class Method Detail

def self.build(**tuple : **T) forall T #

Build a new empty model and fill the columns using the NamedTuple in argument.

Returns the new model


[View source]
def self.build(**tuple) #

Build a new empty model and fill the columns using the NamedTuple in argument.

Returns the new model


[View source]
def self.build(**tuple, &) #

Build a new empty model and fill the columns using the NamedTuple in argument.

Returns the new model


[View source]
def self.columns #

[View source]
def self.connection : String #

Define on which connection the model is living. Useful in case of models living in different databases.

Is set to "default" by default.

See Lustra::SQL#init(URI, *opts) for more information about multi-connections.

Example:

Lustra::SQL.init("postgres://postgres@localhost/database_1")
Lustra::SQL.init("secondary", "postgres://postgres@localhost/database_2")

class ModelA
  include Lustra::Model

  # Performs all the queries on `database_1`
  # self.connection = "default"
  column id : Int32, primary: true, presence: false
  column title : String
end

class ModelB
  include Lustra::Model

  # Performs all the queries on `database_2`
  self.connection = "secondary"

  column id : Int32, primary: true, presence: false
end

def self.connection=(connection : String) #

Define on which connection the model is living. Useful in case of models living in different databases.

Is set to "default" by default.

See Lustra::SQL#init(URI, *opts) for more information about multi-connections.

Example:

Lustra::SQL.init("postgres://postgres@localhost/database_1")
Lustra::SQL.init("secondary", "postgres://postgres@localhost/database_2")

class ModelA
  include Lustra::Model

  # Performs all the queries on `database_1`
  # self.connection = "default"
  column id : Int32, primary: true, presence: false
  column title : String
end

class ModelB
  include Lustra::Model

  # Performs all the queries on `database_2`
  self.connection = "secondary"

  column id : Int32, primary: true, presence: false
end

def self.create_from_json(string_or_io : String | IO, trusted : Bool = false) #

Create a new model from json and save it. Returns the model.

The model may not be saved due to validation failure; check the returned model errors? and persisted? flags. Trusted flag set to true will allow mass assignment without protection, FALSE by default


def self.create_from_json!(string_or_io : String | IO, trusted : Bool = false) #

Create a new model from json and save it. Returns the model.

Returns the newly inserted model Raises an exception if validation failed during the saving process. Trusted flag set to true will allow mass assignment without protection, FALSE by default


def self.find(ids : Array) #

Find multiple models by an array of primary keys. Returns an array of models (may be empty if none found).

users = User.find([1, 2, 3])
users.size # => 0..3 depending on how many were found

[View source]
def self.find(x) #

Returns a model using primary key equality Returns nil if not found.


[View source]
def self.find!(ids : Array) #

Find multiple models by an array of primary keys. Raises error if ANY of the IDs are not found.

users = User.find!([1, 2, 3]) # Raises if any ID is not found

[View source]
def self.find!(x) #

Returns a model using primary key equality. Raises error if the model is not found.


[View source]
def self.find_by(tuple : NamedTuple) #

Find a model by column values. Returns nil if not found. This is an alias for query.find(**tuple) with better naming.

user = User.find_by(email: "[email protected]")
user = User.find_by(first_name: "John", last_name: "Doe")

[View source]
def self.find_by(**tuple) #

Find a model by column values. Returns nil if not found. This is an alias for query.find(**tuple) with better naming.

user = User.find_by(email: "[email protected]")
user = User.find_by(first_name: "John", last_name: "Doe")

[View source]
def self.find_by!(tuple : NamedTuple) #

Find a model by column values. Raises error if not found. This is an alias for query.find!(**tuple) with better naming.

user = User.find_by!(email: "[email protected]")

[View source]
def self.find_by!(**tuple) #

Find a model by column values. Raises error if not found. This is an alias for query.find!(**tuple) with better naming.

user = User.find_by!(email: "[email protected]")

[View source]
def self.from_json(string_or_io : String | IO, trusted : Bool = false) #

Create a new empty model and fill the columns from json. Returns the new model

Trusted flag set to true will allow mass assignment without protection, FALSE by default


def self.full_table_name #

returns the fully qualified and escaped name for this table. add schema if schema is different from 'public' (default schema)

ex: "schema"."table"


[View source]
def self.import(array : Enumerable(self), on_conflict : Lustra::SQL::InsertQuery -> | Nil = nil) #

Import a bulk of models in one SQL insert query. Each model must be non-persisted.

on_conflict callback can be optionnaly turned on to manage constraints of the database.

Note: Old models are not modified. This method return a copy of the models as saved in the database.

Example:

users = [User.new(id: 1), User.new(id: 2), User.new(id: 3)]
users = User.import(users)

[View source]
def self.polymorphic? : Bool #

def self.query #

Return a new query SELECT * FROM [my_model_table]. Can be refined after that. Automatically applies default_scope if defined.


[View source]
def self.read_only=(read_only : Bool) #

def self.read_only? : Bool #

def self.register_counter_cache(association_model : Class, counter_column : String, foreign_key : String) #

Register a counter cache for this model using model class


[View source]
def self.reset_counters(id, *counter_models) #

Reset counter cache columns to their correct values. This is useful when counter caches become out of sync due to direct SQL operations.

Example:

User.reset_counters(user.id, Post)
User.reset_counters(user.id, Post, Comment)

[View source]
def self.schema : Lustra::SQL::Symbolic | Nil #

Define the current schema used in PostgreSQL. The value is nil by default, which lead to non-specified schema during the querying, and usage of "public" by PostgreSQL.

This property can be redefined on initialization. Example:

class MyModel
  include Lustra::Model

  self.schema = "my_schema"
end
MyModel.query.to_sql # SELECT * FROM "my_schema"."my_models"

def self.schema=(schema : Lustra::SQL::Symbolic | Nil) #

Define the current schema used in PostgreSQL. The value is nil by default, which lead to non-specified schema during the querying, and usage of "public" by PostgreSQL.

This property can be redefined on initialization. Example:

class MyModel
  include Lustra::Model

  self.schema = "my_schema"
end
MyModel.query.to_sql # SELECT * FROM "my_schema"."my_models"

def self.table : Lustra::SQL::Symbolic #

Return the table name setup for this model. By convention, the class name is by default equals to the pluralized underscored string form of the model name. Example:

MyModel => "my_models"
Person => "people"
Project::Info => "project_infos"

The property can be updated at initialization to a custom table name:

class MyModel
  include Lustra::Model

  self.table = "another_table_name"
end
MyModel.query.to_sql # SELECT * FROM "another_table_name"

def self.table=(table : Lustra::SQL::Symbolic) #

Return the table name setup for this model. By convention, the class name is by default equals to the pluralized underscored string form of the model name. Example:

MyModel => "my_models"
Person => "people"
Project::Info => "project_infos"

The property can be updated at initialization to a custom table name:

class MyModel
  include Lustra::Model

  self.table = "another_table_name"
end
MyModel.query.to_sql # SELECT * FROM "another_table_name"

Instance Method Detail

def _cached_user : User | Nil #

def add_built_association(association_name : String, model : Lustra::Model) #

Add a built association to track for autosave


[View source]
def attributes : Hash(String, Lustra::SQL::Any) #

Attributes, used when fetch_columns is true


def built_associations : Hash(String, Array(Lustra::Model)) #

Track built associations for autosave functionality


def built_associations=(built_associations : Hash(String, Array(Lustra::Model))) #

Track built associations for autosave functionality


def cache : Lustra::Model::QueryCache | Nil #

def changed : Array(String) #

Returns an array of names of all changed attributes.

user.email = "[email protected]"
user.first_name = "John"
user.changed  # => ["email", "first_name"]

def changed? #

Return true if the model is dirty (e.g. one or more fields have been changed.). Return false otherwise.


def changes : Hash(String, Tuple(Lustra::SQL::Any, Lustra::SQL::Any)) #

Returns a hash of all changed attributes with their [old_value, new_value].

user.email = "[email protected]"
user.first_name = "John"
user.changes  # => {"email" => ["[email protected]", "[email protected]"], "first_name" => [nil, "John"]}

def clear_built_associations #

Clear all built associations (called after successful save)


[View source]
def clear_change_flags #

Reset the #changed? flag on all columns

The model behave like its not dirty anymore and call to save would apply no changes.

Returns self


def created_at : Time #

Returns the value of #created_at column or throw an exception if the column is not defined.


def created_at=(x : Time) #

Setter for #created_at column.


def created_at_column : Lustra::Model::Column(Time, Lustra::Model::Converter::TimeConverter) #

Returns the column object used to manage #created_at field

See Lustra::Model::Column


def has_built_associations? : Bool #

Check if there are any pending built associations


[View source]
def id : Int64 #

Returns the value of #id column or throw an exception if the column is not defined.


def id=(x : Int64) #

Setter for #id column.


def id_column : Lustra::Model::Column(Int64, Lustra::Model::Converter::Int64Converter) #

Returns the column object used to manage #id field

See Lustra::Model::Column


def location : PG::Geo::Point | Nil #

Returns the value of #location column or throw an exception if the column is not defined.


def location=(x : PG::Geo::Point | Nil) #

Setter for #location column.


def location_column : Lustra::Model::Column(PG::Geo::Point | Nil, Lustra::Model::Converter::PGGeoPointConverter) #

Returns the column object used to manage #location field

See Lustra::Model::Column


def message : String #

[View source]
def reset(h : Hash(Symbol, _)) #

Set the columns from hash


def reset(h : Hash(String, _)) #

Set the model fields from hash


def reset(t : NamedTuple) #

def reset(from_json : JSON::Any) #

def reset(**t : **T) forall T #

reset flavors


def reset_counters(*counter_models) #

Reset counter cache columns

Example:

user = User.find(1)
user.reset_counters(Post)
user.reset_counters(Post, Comment)

[View source]
def set(h : Hash(Symbol, _)) #

Set the columns from hash


def set(h : Hash(String, _)) #

Set the model fields from hash


def set(t : NamedTuple) #

def set(from_json : JSON::Any) #

def set(**t : **T) forall T #
Description copied from module Lustra::Model::HasColumns

Set one or multiple columns to a specific value This two are equivalents:

model.set(a: 1)
model.a = 1

def set_from_json(string_or_io : String | IO, trusted : Bool = false) #

Set the fields from json passed as argument Trusted flag set to true will allow mass assignment without protection, FALSE by default


def telegram_chat_id : Int64 #

Returns the value of #telegram_chat_id column or throw an exception if the column is not defined.


def telegram_chat_id=(x : Int64) #

Setter for #telegram_chat_id column.


def telegram_chat_id_column : Lustra::Model::Column(Int64, Lustra::Model::Converter::Int64Converter) #

Returns the column object used to manage #telegram_chat_id field

See Lustra::Model::Column


def telegram_chat_type : String #

Returns the value of #telegram_chat_type column or throw an exception if the column is not defined.


def telegram_chat_type=(x : String) #

Setter for #telegram_chat_type column.


def telegram_chat_type_column : Lustra::Model::Column(String, Lustra::Model::Converter::StringConverter) #

Returns the column object used to manage #telegram_chat_type field

See Lustra::Model::Column


def telegram_message_date : Int64 #

Returns the value of #telegram_message_date column or throw an exception if the column is not defined.


def telegram_message_date=(x : Int64) #

Setter for #telegram_message_date column.


def telegram_message_date_column : Lustra::Model::Column(Int64, Lustra::Model::Converter::Int64Converter) #

Returns the column object used to manage #telegram_message_date field

See Lustra::Model::Column


def telegram_message_id : Int64 #

Returns the value of #telegram_message_id column or throw an exception if the column is not defined.


def telegram_message_id=(x : Int64) #

Setter for #telegram_message_id column.


def telegram_message_id_column : Lustra::Model::Column(Int64, Lustra::Model::Converter::Int64Converter) #

Returns the column object used to manage #telegram_message_id field

See Lustra::Model::Column


def text : String | Nil #

Returns the value of #text column or throw an exception if the column is not defined.


def text=(x : String | Nil) #

Setter for #text column.


def text_column : Lustra::Model::Column(String | Nil, Lustra::Model::Converter::StringConverter) #

Returns the column object used to manage #text field

See Lustra::Model::Column


def to_h(full = false) : Hash(String, Lustra::SQL::Any) #

Return a hash version of the columns of this model.


def to_json(emit_nulls : Bool = false) #

def to_json(json, emit_nulls = false) #

def touch(time : Time = Time.local) : Lustra::Model #

Updates timestamp columns without triggering validations or callbacks.

user.touch             # Updates updated_at
user.touch(2.days.ago) # Updates updated_at to specific time

[View source]
def touch(column : Symbol | String, time : Time = Time.local) : Lustra::Model #

Updates the specified column and updated_at without triggering validations or callbacks.

user.touch(:last_login_at) # Updates last_login_at and updated_at
user.touch(:last_seen_at, 1.hour.ago)

[View source]
def touch(columns : Array(Symbol | String), time : Time = Time.local) : Lustra::Model #

Updates multiple timestamp columns without triggering validations or callbacks.

user.touch([:last_login_at, :last_seen_at])
user.touch([:last_login_at, :last_seen_at], 3.days.ago)

[View source]
def touch(*columns, time : Time = Time.local) : Lustra::Model #

Updates multiple timestamp columns at once.

user.touch(:last_login_at, :last_seen_at)
user.touch(:last_login_at, :last_seen_at, time: 1.day.ago)

[View source]
def update_from_json(string_or_io : String | IO, trusted : Bool = false) #

Set the fields from json passed as argument and call save on the object Trusted flag set to true will allow mass assignment without protection, FALSE by default


def update_from_json!(string_or_io : String | IO, trusted : Bool = false) #

Set the fields from json passed as argument and call save! on the object Trusted flag set to true will allow mass assignment without protection, FALSE by default


def update_h : Hash(String, Lustra::SQL::Any) #

Generate the hash for update request (like during save)


def updated_at : Time #

Returns the value of #updated_at column or throw an exception if the column is not defined.


def updated_at=(x : Time) #

Setter for #updated_at column.


def updated_at_column : Lustra::Model::Column(Time, Lustra::Model::Converter::TimeConverter) #

Returns the column object used to manage #updated_at field

See Lustra::Model::Column


def user : User #

The method user is a belongs_to relation to User


def user=(model : User) #

def user_id : Int64 #

Returns the value of #user_id column or throw an exception if the column is not defined.


def user_id=(x : Int64) #

Setter for #user_id column.


def user_id_column : Lustra::Model::Column(Int64, Lustra::Model::Converter::Int64Converter) #

Returns the column object used to manage #user_id field

See Lustra::Model::Column


def validate_fields_presence #

For each column, ensure than when needed the column has present information into it.

This method is called on validation.