Skip to content

Instantly share code, notes, and snippets.

@watzon
Last active June 18, 2019 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save watzon/933ea33bb474d3b3f46ac5434a652a2c to your computer and use it in GitHub Desktop.
Save watzon/933ea33bb474d3b3f46ac5434a652a2c to your computer and use it in GitHub Desktop.
Lucky Models
class Organization < BaseModel
table :organizations do
column name : String
column email : String
column profile_image_url : String?
column description : String?
column github_username : String?
column gitlab_username : String?
column twitter_username : String?
column website_url : String?
has_many organization_members : OrganizationMember
has_many members : User, through: :organization_members
end
def profile_photo
if profile_image = profile_image_url
profile_image
else
"https://placeimg.com/300/300/arch/grayscale"
end
end
end
class OrganizationMember < BaseModel
table :organization_members do
belongs_to user : User
belongs_to organization : Organization
end
end
class Shard < BaseModel
table :shards do
column name : String
column description : String?
column readme_content : String?
column spdx_license_identifier : String?
column license_url : String?
column license_content : String?
column repository_url : String
column repository_type : String
column documentation_url : String?
column bug_tracker_url : String?
column authors : String?
has_many downloads : ShardDownload
has_many versions : ShardVersion
column shardable_type : String
column shardable_id : Int32
end
def shardable
if shardable_type == "organization"
OrganizationQuery.find(shardable_id)
elsif shardable_type == "user"
UserQuery.find(shardable_id)
else
raise "Unexpected shardable_type: #{shardable_type}"
end
end
end
class ShardDownload < BaseModel
# Use ipstack for getting info about the user
table :shard_downloads do
column ip_columnress : String
column user_agent : String
column continent_code : String?
column continent_name : String?
column country_code : String?
column country_name : String?
column region_code : String?
column region_name : String?
column city : String?
column zip : Int32?
column time_zone : String?
column time_zone_code : String?
column isp : String?
column proxied : Bool?
column crawler : Bool?
column crawler_name : String?
column crawler_type : String?
column tor : Bool?
belongs_to user : User?
belongs_to shard_version : ShardVersion
end
end
class ShardVersion < BaseModel
table :shard_versions do
column name : String
column ghid : Int32
column node_id : String
column url : String
column description : String?
column prerelease : Bool
column published_at : Time
column tarball_url : String
belongs_to shard : Shard
end
end
class User < BaseModel
include Carbon::Emailable
include Authentic::PasswordAuthenticatable
table :users do
column email : String
column username : String
column full_name : String?
column encrypted_password : String
column github_username : String?
column gitlab_username : String?
column twitter_username : String?
column personal_website : String?
column newsletter : Bool = false
column superuser : Bool?
has_many organization_members : OrganizationMember
has_many organizations : Organization, through: :organization_members
end
def is_superuser?
@superuser
end
def profile_photo
GravatarHelper.gravatar_for_email(email)
end
def emailable
Carbon::Address.new(email)
end
end
@watzon
Copy link
Author

watzon commented Jun 18, 2019

Error:

Error in tasks.cr:2: while requiring "./src/app"

require "./src/app"
^

in src/app.cr:9: while requiring "./models/**"

require "./models/**"
^

in src/models/organization_member.cr:3: expanding macro

    belongs_to user : User
    ^~~~~~~~~~

in lib/avram/src/avram/associations.cr:66: undefined constant User

    {% owner_id_type = model.resolve.constant(:PRIMARY_KEY_TYPE_CLASS) %}
                             ^~~~~~~

in src/models/organization_member.cr:3: undefined constant User

    belongs_to user : User
                      ^~~~

Error in tasks.cr:2: while requiring "./src/app"

require "./src/app"
^

in src/app.cr:9: while requiring "./models/**"

require "./models/**"
^

in src/models/organization_member.cr:3: expanding macro

    belongs_to user : User
    ^~~~~~~~~~

in lib/avram/src/avram/associations.cr:66: undefined constant User

    {% owner_id_type = model.resolve.constant(:PRIMARY_KEY_TYPE_CLASS) %}
                             ^~~~~~~

in src/models/organization_member.cr:3: undefined constant User

    belongs_to user : User
                      ^~~~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment