class Admin
- Admin
- Reference
- Object
Included Modules
- Clear::Model
Extended Modules
- Clear::Model::HasHooks::ClassMethods
Defined in:
models/admin.crConstant Summary
-
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}, "uid" => {type: String, primary: false, converter: "String", db_column_name: "uid", crystal_variable_name: uid, presence: true, mass_assign: true}, "raw_json" => {type: String, primary: false, converter: "String", db_column_name: "raw_json", crystal_variable_name: raw_json, presence: true, mass_assign: true}, "role" => {type: Int32, primary: false, converter: "Int32", db_column_name: "role", crystal_variable_name: role, presence: true, mass_assign: true}, "name" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "name", crystal_variable_name: name, presence: true, mass_assign: true}, "email" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "email", crystal_variable_name: email, presence: true, mass_assign: true}, "nickname" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "nickname", crystal_variable_name: nickname, presence: true, mass_assign: true}, "first_name" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "first_name", crystal_variable_name: first_name, presence: true, mass_assign: true}, "last_name" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "last_name", crystal_variable_name: last_name, presence: true, mass_assign: true}, "location" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "location", crystal_variable_name: location, presence: true, mass_assign: true}, "image" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "image", crystal_variable_name: image, presence: true, mass_assign: true}, "phone" => {type: String | ::Nil, primary: false, converter: "String", db_column_name: "phone", crystal_variable_name: phone, 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}} of Nil => Nil
-
POLYMORPHISM_SETTINGS =
{} of Nil => Nil
-
ROLES =
{0 => "guest", 1 => "admin"}
Constructors
-
.build(x : NamedTuple) : self
Build a new empty model and fill the columns using the NamedTuple in argument.
-
.build(x : NamedTuple, &block : self -> Nil) : self
Build a new empty model and fill the columns using the NamedTuple in argument.
-
.create(x : NamedTuple, &block : self -> Nil) : self
Build and new model and save it.
-
.create(**tuple, &block : self -> Nil) : self
Build and new model and save it.
-
.create(x : NamedTuple) : self
Build and new model and save it.
-
.create(**tuple) : self
Build and new model and save it.
-
.create!(x : NamedTuple, &block : self -> Nil) : self
Build and new model and save it.
-
.create!(**tuple, &block : self -> Nil) : self
Build and new model and save it.
-
.create!(x : NamedTuple) : self
Build and new model and save it.
-
.create!(**tuple) : self
Build and new model and save it.
- .new(h : Hash(String, _), cache : Clear::Model::QueryCache | Nil = nil, persisted = false, fetch_columns = false)
- .new(json : JSON::Any, cache : Clear::Model::QueryCache | Nil = nil, persisted = false)
- .new(t : NamedTuple, persisted = false)
- .new
Class Method Summary
-
.build(**tuple : **T) forall T
Build a new empty model and fill the columns using the NamedTuple in argument.
-
.build(**tuple)
Build a new empty model and fill the columns using the NamedTuple in argument.
-
.build(**tuple, &)
Build a new empty model and fill the columns using the NamedTuple in argument.
- .columns
-
.connection : String
Define on which connection the model is living.
-
.connection=(connection : String)
Define on which connection the model is living.
-
.create_from_json(string_or_io : String | IO, trusted : Bool = false)
Create a new model from json and save it.
-
.create_from_json!(string_or_io : String | IO, trusted : Bool = false)
Create a new model from json and save it.
-
.find(x)
Returns a model using primary key equality Returns
nil
if not found. -
.find!(x)
Returns a model using primary key equality.
-
.from_json(string_or_io : String | IO, trusted : Bool = false)
Create a new empty model and fill the columns from json.
-
.full_table_name
returns the fully qualified and escaped name for this table.
-
.import(array : Enumerable(self), on_conflict : Clear::SQL::InsertQuery -> | Nil = nil)
Import a bulk of models in one SQL insert query.
- .polymorphic? : Bool
-
.query
Return a new empty query
SELECT * FROM [my_model_table]
. - .read_only=(read_only : Bool)
- .read_only? : Bool
-
.schema : Clear::SQL::Symbolic | Nil
Define the current schema used in PostgreSQL.
-
.schema=(schema : Clear::SQL::Symbolic | Nil)
Define the current schema used in PostgreSQL.
-
.table : Clear::SQL::Symbolic
Return the table name setup for this model.
-
.table=(table : Clear::SQL::Symbolic)
Return the table name setup for this model.
Instance Method Summary
- #admin? : Bool
-
#attributes : Hash(String, Clear::SQL::Any)
Attributes, used when fetch_columns is true
- #cache : Clear::Model::QueryCache | Nil
-
#changed?
Return
true
if the model is dirty (e.g. -
#clear_change_flags
Reset the
#changed?
flag on all columns -
#created_at : Time
Returns the value of
#created_at
column or throw an exception if the column is not defined. -
#created_at=(x : Time)
Setter for
#created_at
column. -
#created_at_column : Clear::Model::Column(Time, Clear::Model::Converter::TimeConverter)
Returns the column object used to manage
#created_at
field - #decorate
-
#email : String | Nil
Returns the value of
#email
column or throw an exception if the column is not defined. -
#email=(x : String | Nil)
Setter for
#email
column. -
#email_column : Clear::Model::Column(String | Nil, Clear::Model::Converter::StringConverter)
Returns the column object used to manage
#email
field -
#first_name : String | Nil
Returns the value of
#first_name
column or throw an exception if the column is not defined. -
#first_name=(x : String | Nil)
Setter for
#first_name
column. -
#first_name_column : Clear::Model::Column(String | Nil, Clear::Model::Converter::StringConverter)
Returns the column object used to manage
#first_name
field - #guest? : Bool
-
#id : Int64
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 : Clear::Model::Column(Int64, Clear::Model::Converter::Int64Converter)
Returns the column object used to manage
#id
field -
#image : String | Nil
Returns the value of
#image
column or throw an exception if the column is not defined. -
#image=(x : String | Nil)
Setter for
#image
column. -
#image_column : Clear::Model::Column(String | Nil, Clear::Model::Converter::StringConverter)
Returns the column object used to manage
#image
field -
#invalidate_caching : self
Force to clean-up the caches for the relations connected to this model.
-
#last_name : String | Nil
Returns the value of
#last_name
column or throw an exception if the column is not defined. -
#last_name=(x : String | Nil)
Setter for
#last_name
column. -
#last_name_column : Clear::Model::Column(String | Nil, Clear::Model::Converter::StringConverter)
Returns the column object used to manage
#last_name
field -
#location : String | Nil
Returns the value of
#location
column or throw an exception if the column is not defined. -
#location=(x : String | Nil)
Setter for
#location
column. -
#location_column : Clear::Model::Column(String | Nil, Clear::Model::Converter::StringConverter)
Returns the column object used to manage
#location
field -
#name : String | Nil
Returns the value of
#name
column or throw an exception if the column is not defined. -
#name=(x : String | Nil)
Setter for
#name
column. -
#name_column : Clear::Model::Column(String | Nil, Clear::Model::Converter::StringConverter)
Returns the column object used to manage
#name
field -
#nickname : String | Nil
Returns the value of
#nickname
column or throw an exception if the column is not defined. -
#nickname=(x : String | Nil)
Setter for
#nickname
column. -
#nickname_column : Clear::Model::Column(String | Nil, Clear::Model::Converter::StringConverter)
Returns the column object used to manage
#nickname
field -
#phone : String | Nil
Returns the value of
#phone
column or throw an exception if the column is not defined. -
#phone=(x : String | Nil)
Setter for
#phone
column. -
#phone_column : Clear::Model::Column(String | Nil, Clear::Model::Converter::StringConverter)
Returns the column object used to manage
#phone
field -
#provider : String
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 : Clear::Model::Column(String, Clear::Model::Converter::StringConverter)
Returns the column object used to manage
#provider
field -
#raw_json : String
Returns the value of
#raw_json
column or throw an exception if the column is not defined. -
#raw_json=(x : String)
Setter for
#raw_json
column. -
#raw_json_column : Clear::Model::Column(String, Clear::Model::Converter::StringConverter)
Returns the column object used to manage
#raw_json
field -
#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(**t : **T) forall T
reset flavors
-
#role : Int32
Returns the value of
#role
column or throw an exception if the column is not defined. -
#role=(x : Int32)
Setter for
#role
column. -
#role_column : Clear::Model::Column(Int32, Clear::Model::Converter::Int32Converter)
Returns the column object used to manage
#role
field -
#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(**t : **T) forall T
Set one or multiple columns to a specific value This two are equivalents:
-
#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
-
#to_h(full = false) : Hash(String, Clear::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(now = Time.local) : Clear::Model
Saves the record with the updated_at set to the current time.
-
#uid : String
Returns the value of
#uid
column or throw an exception if the column is not defined. -
#uid=(x : String)
Setter for
#uid
column. -
#uid_column : Clear::Model::Column(String, Clear::Model::Converter::StringConverter)
Returns the column object used to manage
#uid
field -
#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 : Hash(String, Clear::SQL::Any)
Generate the hash for update request (like during save)
-
#updated_at : Time
Returns the value of
#updated_at
column or throw an exception if the column is not defined. -
#updated_at=(x : Time)
Setter for
#updated_at
column. -
#updated_at_column : Clear::Model::Column(Time, Clear::Model::Converter::TimeConverter)
Returns the column object used to manage
#updated_at
field -
#validate_fields_presence
For each column, ensure than when needed the column has present information into it.
Constructor Detail
Build a new empty model and fill the columns using the NamedTuple in argument.
Returns the new model
Build a new empty model and fill the columns using the NamedTuple in argument.
Returns the new model
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.
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.
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.
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.
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.
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.
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.
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.
Class Method Detail
Build a new empty model and fill the columns using the NamedTuple in argument.
Returns the new model
Build a new empty model and fill the columns using the NamedTuple in argument.
Returns the new model
Build a new empty model and fill the columns using the NamedTuple in argument.
Returns the new model
Define on which connection the model is living. Useful in case of models living in different databases.
Is set to "default" by default.
See Clear::SQL#init(URI, *opts)
for more information about multi-connections.
Example:
Clear::SQL.init("postgres://postgres@localhost/database_1")
Clear::SQL.init("secondary", "postgres://postgres@localhost/database_2")
class ModelA
include Clear::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 Clear::Model
# Performs all the queries on `database_2`
self.connection = "secondary"
column id : Int32, primary: true, presence: false
end
Define on which connection the model is living. Useful in case of models living in different databases.
Is set to "default" by default.
See Clear::SQL#init(URI, *opts)
for more information about multi-connections.
Example:
Clear::SQL.init("postgres://postgres@localhost/database_1")
Clear::SQL.init("secondary", "postgres://postgres@localhost/database_2")
class ModelA
include Clear::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 Clear::Model
# Performs all the queries on `database_2`
self.connection = "secondary"
column id : Int32, primary: true, presence: false
end
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 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
Returns a model using primary key equality. Raises error if the model is not found.
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
returns the fully qualified and escaped name for this table. add schema if schema is different from 'public' (default schema)
ex: "schema"."table"
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)
Return a new empty query SELECT * FROM [my_model_table]
. Can be refined after that.
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 Clear::Model
self.schema = "my_schema"
end
MyModel.query.to_sql # SELECT * FROM "my_schema"."my_models"
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 Clear::Model
self.schema = "my_schema"
end
MyModel.query.to_sql # SELECT * FROM "my_schema"."my_models"
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 Clear::Model
self.table = "another_table_name"
end
MyModel.query.to_sql # SELECT * FROM "another_table_name"
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 Clear::Model
self.table = "another_table_name"
end
MyModel.query.to_sql # SELECT * FROM "another_table_name"
Instance Method Detail
Return true
if the model is dirty (e.g. one or more fields
have been changed.). Return false
otherwise.
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
Returns the value of #created_at
column or throw an exception if the column is not defined.
Returns the column object used to manage #created_at
field
See Clear::Model::Column
Returns the value of #email
column or throw an exception if the column is not defined.
Returns the column object used to manage #email
field
See Clear::Model::Column
Returns the value of #first_name
column or throw an exception if the column is not defined.
Returns the column object used to manage #first_name
field
See Clear::Model::Column
Returns the value of #id
column or throw an exception if the column is not defined.
Returns the column object used to manage #id
field
See Clear::Model::Column
Returns the value of #image
column or throw an exception if the column is not defined.
Returns the column object used to manage #image
field
See Clear::Model::Column
Force to clean-up the caches for the relations connected to this model.
Returns the value of #last_name
column or throw an exception if the column is not defined.
Returns the column object used to manage #last_name
field
See Clear::Model::Column
Returns the value of #location
column or throw an exception if the column is not defined.
Returns the column object used to manage #location
field
See Clear::Model::Column
Returns the value of #name
column or throw an exception if the column is not defined.
Returns the column object used to manage #name
field
See Clear::Model::Column
Returns the value of #nickname
column or throw an exception if the column is not defined.
Returns the column object used to manage #nickname
field
See Clear::Model::Column
Returns the value of #phone
column or throw an exception if the column is not defined.
Returns the column object used to manage #phone
field
See Clear::Model::Column
Returns the value of #provider
column or throw an exception if the column is not defined.
Returns the column object used to manage #provider
field
See Clear::Model::Column
Returns the value of #raw_json
column or throw an exception if the column is not defined.
Returns the column object used to manage #raw_json
field
See Clear::Model::Column
Returns the value of #role
column or throw an exception if the column is not defined.
Returns the column object used to manage #role
field
See Clear::Model::Column
Set one or multiple columns to a specific value This two are equivalents:
model.set(a: 1)
model.a = 1
Set the fields from json passed as argument Trusted flag set to true will allow mass assignment without protection, FALSE by default
Return a hash version of the columns of this model.
Saves the record with the updated_at set to the current time.
Returns the value of #uid
column or throw an exception if the column is not defined.
Returns the column object used to manage #uid
field
See Clear::Model::Column
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
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
Generate the hash for update request (like during save)
Returns the value of #updated_at
column or throw an exception if the column is not defined.
Returns the column object used to manage #updated_at
field
See Clear::Model::Column
For each column, ensure than when needed the column has present information into it.
This method is called on validation.