class

Repository

Inherits HasProvider < Lustra::Model < Lustra::Model::FullTextSearchable < Lustra::Model::JSONDeserialize < Lustra::Model::Initializer < Lustra::Model::HasFactory < Lustra::Model::Introspection < Lustra::Model::ClassMethods < Lustra::Model::HasScope < Lustra::Model::HasRelations < Lustra::Model::HasValidation < Lustra::Validation::Helper < Lustra::Model::HasSaving < Lustra::Model::HasSerialPkey < Lustra::Model::HasTimestamps < Lustra::Model::HasColumns < Lustra::Model::HasHooks < Lustra::Model::Connection < Lustra::ErrorMessages < Reference < Object

Constants

COLUMNS = {"id" => {type: Int64, primary: true, converter: "Int64", db_column_name: "id", crystal_variable_name: id, presence: false, mass_assign: true}, "provider" => {type: String, primary: false, converter: "String", db_column_name: "provider", crystal_variable_name: provider, presence: true, mass_assign: true}, "provider_id" => {type: Int32, primary: false, converter: "Int32", db_column_name: "provider_id", crystal_variable_name: provider_id, presence: true, mass_assign: true}, "name" => {type: String, primary: false, converter: "String", db_column_name: "name", crystal_variable_name: name, presence: true, mass_assign: true}, "description" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "description", crystal_variable_name: description, presence: true, mass_assign: true}, "default_branch" => {type: String, primary: false, converter: "String", db_column_name: "default_branch", crystal_variable_name: default_branch, presence: true, mass_assign: true}, "shard_yml" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "shard_yml", crystal_variable_name: shard_yml, presence: true, mass_assign: true}, "readme" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "readme", crystal_variable_name: readme, presence: true, mass_assign: true}, "changelog" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "changelog", crystal_variable_name: changelog, presence: true, mass_assign: true}, "license" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "license", crystal_variable_name: license, presence: true, mass_assign: true}, "last_activity_at" => {type: Time, primary: false, converter: "Time", db_column_name: "last_activity_at", crystal_variable_name: last_activity_at, presence: true, mass_assign: true}, "stars_count" => {type: Int32, primary: false, converter: "Int32", db_column_name: "stars_count", crystal_variable_name: stars_count, presence: true, mass_assign: true}, "forks_count" => {type: Int32, primary: false, converter: "Int32", db_column_name: "forks_count", crystal_variable_name: forks_count, presence: true, mass_assign: true}, "open_issues_count" => {type: Int32 | ::Nil, primary: false, converter: "Int32", db_column_name: "open_issues_count", crystal_variable_name: open_issues_count, presence: true, mass_assign: true}, "archived" => {type: Bool, primary: false, converter: "Bool", db_column_name: "archived", crystal_variable_name: archived, presence: true, mass_assign: true}, "ignore" => {type: Bool, primary: false, converter: "Bool", db_column_name: "ignore", crystal_variable_name: ignore, presence: true, mass_assign: true}, "fork" => {type: Bool, primary: false, converter: "Bool", db_column_name: "fork", crystal_variable_name: fork, presence: true, mass_assign: true}, "synced_at" => {type: Time, primary: false, converter: "Time", db_column_name: "synced_at", crystal_variable_name: synced_at, presence: true, mass_assign: true}, "created_at" => {type: Time | ::Nil, primary: false, converter: "Time", db_column_name: "created_at", crystal_variable_name: created_at, presence: true, mass_assign: true}, "updated_on" => {type: Time | ::Nil, primary: false, converter: "Time", db_column_name: "updated_on", crystal_variable_name: updated_on, presence: true, mass_assign: true}, "tsv" => {type: Lustra::TSVector, primary: false, converter: "Lustra::TSVector", db_column_name: "tsv", crystal_variable_name: tsv, presence: false, 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

build(x : NamedTuple) : self

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

Returns the new model

Source
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

Source
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.

Source
create

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.

Source
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.

Source
create

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.

Source
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.

Source
create!

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.

Source
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.

Source
create!

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.

Source
new(h : Hash(String, _), cache : Lustra::Model::QueryCache | Nil = nil, persisted = false, fetch_columns = false)
Source
new(json : JSON::Any, cache : Lustra::Model::QueryCache | Nil = nil, persisted = false)
Source
new(t : NamedTuple, persisted = false)
Source

Class methods

build

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

Returns the new model

Source
build

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

Returns the new model

Source
build

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

Returns the new model

Source
columns
Source
connection

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
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
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

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

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
Source
find(x)

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

Source
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
Source
find!(x)

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

Source
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: "test@example.com")
user = User.find_by(first_name: "John", last_name: "Doe")
Source
find_by

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: "test@example.com")
user = User.find_by(first_name: "John", last_name: "Doe")
Source
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: "test@example.com")
Source
find_by!

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: "test@example.com")
Source
find_repository(user_login : String, repository_name : String, provider : String) : Repository | Nil
Source
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

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"

Source
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)
Source
none

Return an empty, chainable collection (Rails-like .none). Useful for conditional branches where no records should be returned while keeping query chaining intact.

User.none.where { active == true }.count # => 0
Source
polymorphic?
published
Source
query

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

Source
read_only=(read_only : Bool)
read_only?
register_counter_cache(association_model : Class, counter_column : String, foreign_key : String)

Register a counter cache for this model using model class

Source
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)
Source
schema

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"
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"
schema_description

Return the table description without printing.

Source
search(str)
table

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"
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"
with_counts
repositories = Repository.query.with_counts

repositories.each(fetch_columns: true) do |repository|
  repository.name
  repository.attributes["dependents_count"]
end
Source
without_releases
Source

Instance methods

__trigger_append_operation_for_association__(association_name : String, models : Array(Lustra::Model))

Handle append operation for through associations (called by autosave system)

_cached_user
add_built_association(association_name : String, model : Lustra::Model)

Add a built association to track for autosave

Source
archived

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

archived=(x : Bool)

Setter for archived column.

archived_column

Returns the column object used to manage archived field

See Lustra::Model::Column

attributes

Attributes, used when fetch_columns is true

built_associations

Track built associations for autosave functionality

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

Track built associations for autosave functionality

cache
changed

Returns an array of names of all changed attributes.

user.email = "new@test.com"
user.first_name = "John"
user.changed  # => ["email", "first_name"]
changed?

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

changelog

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

changelog=(x : String | Nil)

Setter for changelog column.

changelog_column

Returns the column object used to manage changelog field

See Lustra::Model::Column

changes

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

user.email = "new@test.com"
user.first_name = "John"
user.changes  # => {"email" => ["old@test.com", "new@test.com"], "first_name" => [nil, "John"]}
clear_built_associations

Clear all built associations (called after successful save)

Source
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

created_at

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

created_at=(x : Time | Nil)

Setter for created_at column.

created_at_column

Returns the column object used to manage created_at field

See Lustra::Model::Column

decorate
Source
default_branch

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

default_branch=(x : String)

Setter for default_branch column.

default_branch_column

Returns the column object used to manage default_branch field

See Lustra::Model::Column

dependencies
dependents
description

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

description=(x : String | Nil)

Setter for description column.

description_column

Returns the column object used to manage description field

See Lustra::Model::Column

fork

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

fork=(x : Bool)

Setter for fork column.

fork_column

Returns the column object used to manage fork field

See Lustra::Model::Column

forks
forks_count

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

forks_count=(x : Int32)

Setter for forks_count column.

forks_count_column

Returns the column object used to manage forks_count field

See Lustra::Model::Column

has_built_associations?

Check if there are any pending built associations

Source
id

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

id=(x : Int64)

Setter for id column.

id_column

Returns the column object used to manage id field

See Lustra::Model::Column

ignore

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

ignore=(x : Bool)

Setter for ignore column.

ignore_column

Returns the column object used to manage ignore field

See Lustra::Model::Column

language_names
Source
languages
last_activity_at

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

last_activity_at=(x : Time)

Setter for last_activity_at column.

last_activity_at_column

Returns the column object used to manage last_activity_at field

See Lustra::Model::Column

license

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

license=(x : String | Nil)

Setter for license column.

license_column

Returns the column object used to manage license field

See Lustra::Model::Column

name

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

name=(x : String)

Setter for name column.

name_column

Returns the column object used to manage name field

See Lustra::Model::Column

open_issues_count

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

open_issues_count=(x : Int32 | Nil)

Setter for open_issues_count column.

open_issues_count_column

Returns the column object used to manage open_issues_count field

See Lustra::Model::Column

postinstall_script
Source
provider

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

provider=(x : String)

Setter for provider column.

provider_column

Returns the column object used to manage provider field

See Lustra::Model::Column

provider_id

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

provider_id=(x : Int32)

Setter for provider_id column.

provider_id_column

Returns the column object used to manage provider_id field

See Lustra::Model::Column

readme

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

readme=(x : String | Nil)

Setter for readme column.

readme_column

Returns the column object used to manage readme field

See Lustra::Model::Column

releases

The method releases is a has_many relation to Release

repository_languages

The method repository_languages is a has_many relation to RepositoryLanguage

reset(h : Hash(Symbol, _))

Set the columns from hash

reset(h : Hash(String, _))

Set the model fields from hash

reset(t : NamedTuple)
reset(from_json : JSON::Any)
reset

reset flavors

reset_counters(*counter_models)

Reset counter cache columns

Example:

user = User.find(1)
user.reset_counters(Post)
user.reset_counters(Post, Comment)
Source
resync!
Source
set(h : Hash(Symbol, _))

Set the columns from hash

set(h : Hash(String, _))

Set the model fields from hash

set(t : NamedTuple)
set(from_json : JSON::Any)
set

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

model.set(a: 1)
model.a = 1
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

shard_yml

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

shard_yml=(x : String | Nil)

Setter for shard_yml column.

shard_yml_column

Returns the column object used to manage shard_yml field

See Lustra::Model::Column

stars_count

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

stars_count=(x : Int32)

Setter for stars_count column.

stars_count_column

Returns the column object used to manage stars_count field

See Lustra::Model::Column

synced_at

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

synced_at=(x : Time)

Setter for synced_at column.

synced_at_column

Returns the column object used to manage synced_at field

See Lustra::Model::Column

tag_names
Source
tags
tags=(names : Array(String))
Source
to_h(full = false) : Hash(String, Lustra::SQL::Any)

Return a hash version of the columns of this model.

to_json(emit_nulls : Bool = false)
to_json(json, emit_nulls = false)
touch
Source
tsv

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

Setter for tsv column.

tsv_column

Returns the column object used to manage tsv field

See Lustra::Model::Column

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

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

update_h

Generate the hash for update request (like during save)

updated_on

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

updated_on=(x : Time | Nil)

Setter for updated_on column.

updated_on_column

Returns the column object used to manage updated_on field

See Lustra::Model::Column

upstream_repository

Return the related model upstream_repository.

This relation is of type one to zero or one [1, 0..1] between RepositoryFork and Repository

If the relation hasn't been cached, will call a select SQL operation. Otherwise, will try to find in the cache.

upstream_repository!

Return the related model upstream_repository, but throw an error if the model is not found.

user

The method user is a belongs_to relation to User

user=(model : User)
user_id

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

user_id=(x : Int64)

Setter for user_id column.

user_id_column

Returns the column object used to manage user_id field

See Lustra::Model::Column

validate_fields_presence

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

This method is called on validation.

Nested types