diff options
author | Jochen Topf <jochen@topf.org> | 2014-09-06 20:37:13 +0200 |
---|---|---|
committer | Jochen Topf <jochen@topf.org> | 2014-09-06 20:37:13 +0200 |
commit | a841bf2f35bfe477db2458533b8a875cd9cf38e8 (patch) | |
tree | 2ab19aef5c06c65f09d7d5e7406918d6ffc30b9b | |
parent | 95ff801f4829046ca44f949beddb56b066768466 (diff) | |
download | taginfo-a841bf2f35bfe477db2458533b8a875cd9cf38e8.tar taginfo-a841bf2f35bfe477db2458533b8a875cd9cf38e8.tar.gz |
Refactor projects code to work without the Project class.
This way we always get the data dynamically from the database instead of
loading it once at program start. This could allow faster updates in the
future.
-rw-r--r-- | web/lib/projects.rb | 51 | ||||
-rw-r--r-- | web/lib/ui/projects.rb | 9 | ||||
-rw-r--r-- | web/lib/ui/taginfo.rb | 3 | ||||
-rwxr-xr-x | web/taginfo.rb | 2 | ||||
-rw-r--r-- | web/views/project.erb | 20 | ||||
-rw-r--r-- | web/views/taginfo/projects.erb | 32 |
6 files changed, 35 insertions, 82 deletions
diff --git a/web/lib/projects.rb b/web/lib/projects.rb deleted file mode 100644 index 01536a2..0000000 --- a/web/lib/projects.rb +++ /dev/null @@ -1,51 +0,0 @@ -# web/lib/projects.rb -class Project - - @@projects = Array.new - - @@attrs = [:id, :json_url, :fetch_date, :fetch_status, :fetch_json, :fetch_result, :data_format, :data_updated, :data_url, :name, :project_url, :doc_url, :icon_url, :description, :contact_name, :contact_email] - - @@attrs.each do |attr| - attr_reader attr - end - - # Enumerate all available projects - def self.each - @@projects.each do |project| - yield project - end - end - - # Enumerate all available projects - def self.each_with_index - @@projects.each_with_index do |project, n| - yield project, n - end - end - - # The number of available sources - def self.size - @@projects.size - end - - def self.init - db = SQL::Database.new.attach_sources - - db.select("SELECT * FROM projects.projects").execute() do |row| - @@projects << Project.new(row) - end - - db.close - end - - def self.get(id) - @@projects.select{ |p| p.id == id }[0] - end - - def initialize(row) - @@attrs.each do |s| - instance_variable_set("@#{s}", row[s.to_s]) - end - end - -end diff --git a/web/lib/ui/projects.rb b/web/lib/ui/projects.rb index b2eec53..b79bbde 100644 --- a/web/lib/ui/projects.rb +++ b/web/lib/ui/projects.rb @@ -15,12 +15,15 @@ class Taginfo < Sinatra::Base @project_id = params[:project] end - @project = Project.get(@project_id) + @project = @db.select("SELECT * FROM projects.projects"). + condition("id = ?", @project_id).execute()[0] - if @project - @title = [h(@project.name), t.taginfo.projects] + if !@project + halt 404 end + @title = [h(@project['name']), t.taginfo.projects] + section :projects javascript_for(:flexigrid) diff --git a/web/lib/ui/taginfo.rb b/web/lib/ui/taginfo.rb index 710f48f..2f7451b 100644 --- a/web/lib/ui/taginfo.rb +++ b/web/lib/ui/taginfo.rb @@ -110,6 +110,9 @@ class Taginfo < Sinatra::Base @title = t.taginfo.projects @section = 'taginfo' @section_title = t.taginfo.meta + + @projects = @db.select("SELECT * FROM projects.projects ORDER BY name").execute(); + erb :'taginfo/projects' end diff --git a/web/taginfo.rb b/web/taginfo.rb index 58a7fe9..5763fc9 100755 --- a/web/taginfo.rb +++ b/web/taginfo.rb @@ -53,7 +53,6 @@ require 'lib/language.rb' require 'lib/sql.rb' require 'lib/sources.rb' require 'lib/reports.rb' -require 'lib/projects.rb' require 'lib/api.rb' require 'lib/langtag/bcp47.rb' @@ -64,7 +63,6 @@ TaginfoConfig.read #------------------------------------------------------------------------------ DATA_UNTIL = SQL::Database.init('../../data'); -Project.init class Taginfo < Sinatra::Base diff --git a/web/views/project.erb b/web/views/project.erb index faab181..877ff81 100644 --- a/web/views/project.erb +++ b/web/views/project.erb @@ -1,6 +1,6 @@ <div class="pre"> - <h1><%= h(@project.name) %></h1> - <p><%= h(@project.description) %></p> + <h1><%= h(@project['name']) %></h1> + <p><%= h(@project['description']) %></p> </div> <div id="tabs"> <ul class="no-print"> @@ -9,13 +9,13 @@ </ul> <div id="overview"> <table class="desc"> - <tr><th><%= h(t.pages.project.overview.project_name) %>:</th><td><span id="icon_url"></span> <%= h(@project.name) %></td></tr> + <tr><th><%= h(t.pages.project.overview.project_name) %>:</th><td><span id="icon_url"></span> <%= h(@project['name']) %></td></tr> <tr><th><%= h(t.pages.project.overview.project_url) %>:</th><td id="project_url"></td></tr> - <tr><th><%= h(t.pages.project.overview.description) %>:</th><td><%= h(@project.description) %></td></tr> -<% if @project.doc_url %> + <tr><th><%= h(t.pages.project.overview.description) %>:</th><td><%= h(@project['description']) %></td></tr> +<% if @project['doc_url'] %> <tr><th><%= h(t.pages.project.overview.documentation_url) %>:</th><td id="doc_url"></td></tr> <% end %> - <tr><th><%= h(t.pages.project.overview.last_update) %>:</th><td><%= h(@project.data_updated) %></td></tr> + <tr><th><%= h(t.pages.project.overview.last_update) %>:</th><td><%= h(@project['data_updated']) %></td></tr> <tr><th><%= h(t.pages.project.overview.json_url) %>:</th><td id="json_url"></td></tr> </table> <p><i><%= t.pages.project.overview.info %></i></p> @@ -31,10 +31,10 @@ JS.raw(<<"JAVASCRIPT") function page_init2() { var project = #{ @project_id.to_json }, - project_url = #{ @project.project_url.to_json }, - json_url = #{ @project.json_url.to_json }, - icon_url = #{ @project.icon_url.to_json }, - doc_url = #{ @project.doc_url.to_json }; + project_url = #{ @project['project_url'].to_json }, + json_url = #{ @project['json_url'].to_json }, + icon_url = #{ @project['icon_url'].to_json }, + doc_url = #{ @project['doc_url'].to_json }; if (project_url) { jQuery('#project_url').html(link_to_url(project_url)); diff --git a/web/views/taginfo/projects.erb b/web/views/taginfo/projects.erb index dd07ed6..dda4111 100644 --- a/web/views/taginfo/projects.erb +++ b/web/views/taginfo/projects.erb @@ -1,4 +1,4 @@ -<h1 class="section">Projects</h1> +<h1 class="section"><%= h(t.taginfo.projects) %></h1> <table class="list"> <tr> @@ -9,32 +9,32 @@ <th>Project URL<br/>Doc URL</th><th>Contact</th> <th>Description</th> </tr> -<% Project.each_with_index do |project, n| c = (n%2!=0) ? ' even' : '' %> +<% @projects.each_with_index do |project, n| c = (n%2!=0) ? ' even' : '' %> <tr> <td class="tc<%= c %> nowrap"> - <img src="<%= h(project.icon_url) %>" width="16" height="16" alt=""/> + <img src="<%= h(project['icon_url']) %>" width="16" height="16" alt=""/> </td> <td class="<%= c %> nowrap"> - <a href="/projects/<%= h(project.id) %>"><%= h(project.id) %></a><br/> - <%= h(project.name) %> + <a href="/projects/<%= h(project['id']) %>"><%= h(project['id']) %></a><br/> + <%= h(project['name']) %> </td> <td class="<%= c %> nowrap"> - <%= h(project.fetch_date) %><br/> - <%= h(project.data_updated) %> + <%= h(project['fetch_date']) %><br/> + <%= h(project['data_updated']) %> </td> - <td class="tc<%= c %> nowrap"><%= h(project.fetch_status) %></td> - <td class="tc<%= c %> nowrap"><%= h(project.fetch_result) %></td> + <td class="tc<%= c %> nowrap"><%= h(project['fetch_status']) %></td> + <td class="tc<%= c %> nowrap"><%= h(project['fetch_result']) %></td> <td class="<%= c %> nowrap"> - <a href="<%= h(project.json_url) %>"><%= h(project.json_url) %></a><br/> - <a href="<%= h(project.data_url) %>"><%= h(project.data_url) %></a> + <a href="<%= h(project['json_url']) %>"><%= h(project['json_url']) %></a><br/> + <a href="<%= h(project['data_url']) %>"><%= h(project['data_url']) %></a> </td> - <td class="tc<%= c %> nowrap"><%= h(project.data_format) %></td> + <td class="tc<%= c %> nowrap"><%= h(project['data_format']) %></td> <td class="<%= c %> nowrap"> - <a href="<%= h(project.project_url) %>"><%= h(project.project_url) %></a><br/> - <a href="<%= h(project.doc_url) %>"><%= h(project.doc_url) %></a> + <a href="<%= h(project['project_url']) %>"><%= h(project['project_url']) %></a><br/> + <a href="<%= h(project['doc_url']) %>"><%= h(project['doc_url']) %></a> </td> - <td class="<%= c %> nowrap"><%= h(project.contact_name) %><br/><%= h(project.contact_email) %></td> - <td class="<%= c %>"><%= h(project.description) %></td> + <td class="<%= c %> nowrap"><%= h(project['contact_name']) %><br/><%= h(project['contact_email']) %></td> + <td class="<%= c %>"><%= h(project['description']) %></td> </tr> <% end %> </table> |