Skip to content

Instantly share code, notes, and snippets.

@dhoss
Last active October 5, 2015 21:06
Show Gist options
  • Save dhoss/74ccc79ca612a0eded08 to your computer and use it in GitHub Desktop.
Save dhoss/74ccc79ca612a0eded08 to your computer and use it in GitHub Desktop.
sequel models
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Commit ID</th>
<th>Message</th>
<th>Author</th>
<th>Commit Timestamp</th>
</tr>
</thead>
<tbody>
<% @commits.each do |commit| %>
<tr>
<td>
<a href="http://stash.myfamilysouth.com/projects/<%= commit.project.name %>/repos/<%= commit.repository.name %>/commits/<%= commit.git_id %>"><%= commit.git_id %></a>
</td>
<td><%= commit.message %></td>
<td><%= commit.author_info %></td>
<td><%= commit.author_timestamp.to_s %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
class Commit < Sequel::Model
many_to_one :project
many_to_one :repository
# put me in a mixin
def self.list(params)
page = (params[:page] || 1).to_i
limit = (params[:limit] || 100).to_i
self.order(:updated_at).paginate(page, limit)
end
def author_info
"#{author} (#{email})"
end
end
Table "public.commits"
Column | Type | Modifiers
------------------+--------------------------+------------------------------------------------------
id | integer | not null default nextval('commits_id_seq'::regclass)
git_id | character(40) | not null
message | text | not null
author | text | not null
email | text | not null
author_timestamp | timestamp with time zone |
created_at | timestamp with time zone |
updated_at | timestamp with time zone |
project_id | integer |
repository_id | integer |
Indexes:
"commits_pkey" PRIMARY KEY, btree (id)
"commits_git_id_index" UNIQUE, btree (git_id)
Foreign-key constraints:
"commits_project_id_fkey" FOREIGN KEY (project_id) REFERENCES projects(id)
"commits_repository_id_fkey" FOREIGN KEY (repository_id) REFERENCES repositories(id)
class CommitsController < ApplicationController
def index
# I want to make this a common trait so it's not repeated everywhere
page = (params[:page] || 1).to_i
limit = (params[:limit] || 100).to_i
@commits = Commit.eager(:project, :repository).order(:author_timestamp.desc).paginate(page, limit)
@project = Project.where(:name => params[:project_slug]).first
@repository = Repository.where(:name=> params[:repository_slug]).first
end
private
def project_params
params.require(:project).permit(:name, :project_slug, :created_at, :updated_at)
end
end
Started GET "/projects/MEM/repositories/1000memories-main/commits" for 127.0.0.1 at 2015-10-05 14:55:48 -0600
Processing by CommitsController#index as HTML
Parameters: {"project_slug"=>"MEM", "repository_slug"=>"1000memories-main"}
Sequel::Postgres::Database (2.4ms) SELECT count(*) AS "count" FROM "commits" LIMIT 1
Sequel::Postgres::Database (0.4ms) SELECT * FROM "projects" WHERE ("name" = 'MEM') LIMIT 1
Sequel::Postgres::Database (0.2ms) SELECT * FROM "repositories" WHERE ("name" = '1000memories-main') LIMIT 1
Sequel::Postgres::Database (0.8ms) SELECT * FROM "commits" ORDER BY "author_timestamp" DESC LIMIT 100 OFFSET 0
Rendered commits/_commit_list.html.erb (4.0ms)
Rendered commits/index.html.erb within layouts/application (5.0ms)
Completed 500 Internal Server Error in 12ms (Models: 3.8ms)
ActionView::Template::Error (undefined method `project_id' for #<Commit:0x007f11d0047890>):
12: <% @commits.each do |commit| %>
13: <tr>
14: <td>
15: <a href="http://stash.myfamilysouth.com/projects/<%= commit.project.name %>/repos/<%= commit.repository.name %>/commits/<%= commit.git_id %>"><%= commit.git_id %></a>
16: </td>
17: <td><%= commit.message %></td>
18: <td><%= commit.author_info %></td>
app/views/commits/_commit_list.html.erb:15:in `block in _app_views_commits__commit_list_html_erb___1671972169450809021_69857441763600'
app/views/commits/_commit_list.html.erb:12:in `_app_views_commits__commit_list_html_erb___1671972169450809021_69857441763600'
app/views/commits/index.html.erb:3:in `_app_views_commits_index_html_erb__1260205589259250743_69857704639740'
Rendered /home/devin/.gem/ruby/2.1.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (7.2ms)
Rendered /home/devin/.gem/ruby/2.1.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.0ms)
Rendered /home/devin/.gem/ruby/2.1.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
Rendered /home/devin/.gem/ruby/2.1.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (19.2ms)
Rendered /home/devin/.gem/ruby/2.1.3/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.2ms)
Rendered /home/devin/.gem/ruby/2.1.3/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /home/devin/.gem/ruby/2.1.3/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /home/devin/.gem/ruby/2.1.3/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.2ms)
Rendered /home/devin/.gem/ruby/2.1.3/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (10.2ms)
Rendered /home/devin/.gem/ruby/2.1.3/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.3ms)
Rendered /home/devin/.gem/ruby/2.1.3/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.3ms)
Rendered /home/devin/.gem/ruby/2.1.3/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (21.1ms)
class Project < Sequel::Model
one_to_many :repositories
one_to_many :commits
def self.list(params)
page = (params[:page] || 1).to_i
limit = (params[:limit] || 100).to_i
self.order(:updated_at).paginate(page, limit)
end
end
Table "public.projects"
Column | Type | Modifiers
------------+--------------------------+-------------------------------------------------------
id | integer | not null default nextval('projects_id_seq'::regclass)
name | text | not null
created_at | timestamp with time zone | not null
updated_at | timestamp with time zone | not null
Indexes:
"projects_pkey" PRIMARY KEY, btree (id)
"projects_name_index" UNIQUE, btree (name)
Referenced by:
TABLE "commits" CONSTRAINT "commits_project_id_fkey" FOREIGN KEY (project_id) REFERENCES projects(id)
TABLE "repositories" CONSTRAINT "repositories_project_id_fkey" FOREIGN KEY (project_id) REFERENCES projects(id)
Table "public.repositories"
Column | Type | Modifiers
------------+--------------------------+-----------------------------------------------------------
id | integer | not null default nextval('repositories_id_seq'::regclass)
name | text | not null
created_at | timestamp with time zone | not null
updated_at | timestamp with time zone | not null
project_id | integer |
Indexes:
"repositories_pkey" PRIMARY KEY, btree (id)
"repositories_name_index" UNIQUE, btree (name)
Foreign-key constraints:
"repositories_project_id_fkey" FOREIGN KEY (project_id) REFERENCES projects(id)
Referenced by:
TABLE "commits" CONSTRAINT "commits_repository_id_fkey" FOREIGN KEY (repository_id) REFERENCES repositories(id)
class Repository < Sequel::Model
many_to_one :project
one_to_many :commits
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment