Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "kemal"
- require "granite"
- require "granite/adapter/pg"
- require "pg"
- Granite::Adapters << Granite::Adapter::Pg.new({name: "pg", url: "postgresql://postgres:postgres@localhost:5432/indigo2"})
- class User < Granite::Base
- adapter pg
- table_name users
- field username : String
- field nickname : String
- field password : String
- field active : Bool
- field admin : Bool
- timestamps
- has_one :profile
- has_many posts : Post
- validate_not_blank :username
- validate_min_length :username, 3
- validate_max_length :username, 32
- validate_not_blank :nickname
- validate_max_length :nickname, 32
- end
- class Profile < Granite::Base
- adapter pg
- table_name profiles
- field user_id : Int32
- field pronouns : Int32
- field bio : String
- timestamps
- belongs_to :user
- validate_not_blank :pronouns
- validate_max_length :bio, 2000
- end
- class Community < Granite::Base
- adapter pg
- table_name communities
- field title : String
- field description : String
- field icon : String
- field banner : String
- field featured : Bool
- timestamps
- has_many posts : Post
- validate_not_blank :title
- validate_max_length :title, 64
- validate_max_length :description, 2000
- end
- class Post < Granite::Base
- adapter pg
- table_name posts
- field user_id : Int32
- field community_id : Int32
- field body : String
- timestamps
- belongs_to :user
- belongs_to :community
- validate_not_blank :user_id
- validate_not_blank :community_id
- validate_not_blank :body
- validate_max_length :body, 2000
- end
- User.migrator.drop_and_create
- Profile.migrator.drop_and_create
- Community.migrator.drop_and_create
- Post.migrator.drop_and_create
- community = Community.new
- community.title = "ok"
- community.description = "ok"
- community.icon = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/OK-button_-_Macro_photography_of_a_remote_control.jpg/220px-OK-button_-_Macro_photography_of_a_remote_control.jpg"
- community.banner = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/OK-button_-_Macro_photography_of_a_remote_control.jpg/220px-OK-button_-_Macro_photography_of_a_remote_control.jpg"
- community.featured = true
- community.save
- serve_static({"gzip" => true, "dir_listing" => false})
- get "/" do |env|
- env.response.headers["X-PJAX"] = ""
- communities = Community.where(featured: true).limit(4)
- title = "Communities"
- if env.response.headers["X-PJAX"]
- render "src/views/communities/index.ecr", "src/views/layout.ecr"
- else
- render "src/views/communities/index.ecr"
- end
- end
- get "/communities/:id" do |env|
- env.response.headers["X-PJAX"] = ""
- community = Community.where(id: env.params.url["id"]).limit(1)
- title = community.title
- if env.response.headers["X-PJAX"]
- render "src/views/communities/show.ecr", "src/views/layout.ecr"
- else
- render "src/views/communities/show.ecr"
- end
- end
- get "/register" do |env|
- env.response.headers["X-PJAX"] = ""
- title = "Register"
- if env.response.headers["X-PJAX"]
- render "src/views/users/register.ecr", "src/views/layout.ecr"
- else
- render "src/views/users/register.ecr"
- end
- end
- get "/login" do |env|
- env.response.headers["X-PJAX"] = ""
- title = "Login"
- if env.response.headers["X-PJAX"]
- render "src/views/users/login.ecr", "src/views/layout.ecr"
- else
- render "src/views/users/login.ecr"
- end
- end
- Kemal.run
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement