aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/admin_public_body_category_controller.rb55
-rw-r--r--app/controllers/admin_public_body_heading_controller.rb54
-rw-r--r--app/models/public_body_category.rb39
-rw-r--r--app/models/public_body_heading.rb34
-rw-r--r--app/views/admin_general/_admin_navbar.html.erb1
-rw-r--r--app/views/admin_public_body_category/_form.html.erb55
-rw-r--r--app/views/admin_public_body_category/_one_list.html.erb43
-rw-r--r--app/views/admin_public_body_category/edit.html.erb30
-rw-r--r--app/views/admin_public_body_category/index.html.erb16
-rw-r--r--app/views/admin_public_body_category/new.html.erb21
-rw-r--r--app/views/admin_public_body_heading/_form.html.erb41
-rw-r--r--app/views/admin_public_body_heading/edit.html.erb30
-rw-r--r--app/views/admin_public_body_heading/new.html.erb21
13 files changed, 436 insertions, 4 deletions
diff --git a/app/controllers/admin_public_body_category_controller.rb b/app/controllers/admin_public_body_category_controller.rb
new file mode 100644
index 000000000..635e664ee
--- /dev/null
+++ b/app/controllers/admin_public_body_category_controller.rb
@@ -0,0 +1,55 @@
+class AdminPublicBodyCategoryController < AdminController
+ def index
+ @locale = self.locale_from_params
+ @category_headings = PublicBodyHeading.all
+ end
+
+ def new
+ @category = PublicBodyCategory.new
+ render :formats => [:html]
+ end
+
+ def edit
+ @category = PublicBodyCategory.find(params[:id])
+ @tagged_public_bodies = PublicBody.find_by_tag(@category.category_tag)
+ end
+
+ def update
+ I18n.with_locale(I18n.default_locale) do
+ @category = PublicBodyCategory.find(params[:id])
+ if @category.update_attributes(params[:public_body_category])
+ flash[:notice] = 'Category was successfully updated.'
+ end
+ render :action => 'edit'
+ end
+ end
+
+ def create
+ I18n.with_locale(I18n.default_locale) do
+ @category = PublicBodyCategory.new(params[:public_body_category])
+ if @category.save
+ flash[:notice] = 'Category was successfully created.'
+ redirect_to admin_category_index_url
+ else
+ render :action => 'new'
+ end
+ end
+ end
+
+ def destroy
+ @locale = self.locale_from_params
+ I18n.with_locale(@locale) do
+ category = PublicBodyCategory.find(params[:id])
+
+ if PublicBody.find_by_tag(category.category_tag).count > 0
+ flash[:notice] = "There are authorities associated with this category, so can't destroy it"
+ redirect_to admin_category_edit_url(category)
+ return
+ end
+
+ category.destroy
+ flash[:notice] = "Category was successfully destroyed."
+ redirect_to admin_category_index_url
+ end
+ end
+end
diff --git a/app/controllers/admin_public_body_heading_controller.rb b/app/controllers/admin_public_body_heading_controller.rb
new file mode 100644
index 000000000..43d8e329c
--- /dev/null
+++ b/app/controllers/admin_public_body_heading_controller.rb
@@ -0,0 +1,54 @@
+class AdminPublicBodyHeadingController < AdminController
+ def index
+ redirect_to admin_category_index_url
+ end
+
+ def edit
+ @heading = PublicBodyHeading.find(params[:id])
+ render :formats => [:html]
+ end
+
+ def update
+ I18n.with_locale(I18n.default_locale) do
+ @heading = PublicBodyHeading.find(params[:id])
+ if @heading.update_attributes(params[:public_body_heading])
+ flash[:notice] = 'Category heading was successfully updated.'
+ end
+ render :action => 'edit'
+ end
+ end
+
+ def new
+ @heading = PublicBodyHeading.new
+ render :formats => [:html]
+ end
+
+ def create
+ I18n.with_locale(I18n.default_locale) do
+ @heading = PublicBodyHeading.new(params[:public_body_heading])
+ if @heading.save
+ flash[:notice] = 'Category heading was successfully created.'
+ redirect_to admin_category_index_url
+ else
+ render :action => 'new'
+ end
+ end
+ end
+
+ def destroy
+ @locale = self.locale_from_params()
+ I18n.with_locale(@locale) do
+ heading = PublicBodyHeading.find(params[:id])
+
+ if heading.public_body_categories.count > 0
+ flash[:notice] = "There are categories associated with this heading, so can't destroy it"
+ redirect_to admin_heading_edit_url(heading)
+ return
+ end
+
+ heading.destroy
+ flash[:notice] = "Category heading was successfully destroyed."
+ redirect_to admin_category_index_url
+ end
+ end
+end
diff --git a/app/models/public_body_category.rb b/app/models/public_body_category.rb
index 1a88c9d97..c0d8e07c6 100644
--- a/app/models/public_body_category.rb
+++ b/app/models/public_body_category.rb
@@ -11,13 +11,15 @@
require 'forwardable'
class PublicBodyCategory < ActiveRecord::Base
- attr_accessible :locale, :category_tag, :title, :description
+ attr_accessible :locale, :category_tag, :title, :description, :translated_versions
has_and_belongs_to_many :public_body_headings
translates :title, :description
-
- validates_uniqueness_of :category_tag, :message => N_("Tag is already taken")
+ validates_uniqueness_of :category_tag, :message => N_('Tag is already taken')
+ validates_presence_of :title, :message => N_('Title can\'t be blank')
+ validates_presence_of :category_tag, :message => N_('Tag can\'t be blank')
+ validates_presence_of :description, :message => N_('Description can\'t be blank')
def self.get
locale = I18n.locale.to_s || default_locale.to_s || ""
@@ -88,6 +90,37 @@ class PublicBodyCategory < ActiveRecord::Base
end
end
+ # Convenience methods for creating/editing translations via forms
+ def find_translation_by_locale(locale)
+ self.translations.find_by_locale(locale)
+ end
+
+ def skip?(attrs)
+ valueless = attrs.inject({}) { |h, (k, v)| h[k] = v if v != '' and k != 'locale'; h } # because we want to fall back to alternative translations where there are empty values
+ return valueless.length == 0
+ end
+
+ def translated_versions
+ translations
+ end
+
+ def translated_versions=(translation_attrs)
+ if translation_attrs.respond_to? :each_value # Hash => updating
+ translation_attrs.each_value do |attrs|
+ next if skip?(attrs)
+ t = translation_for(attrs[:locale]) || PublicBodyCategory::Translation.new
+ t.attributes = attrs
+ t.save!
+ end
+ else # Array => creating
+ translation_attrs.each do |attrs|
+ next if skip?(attrs)
+ new_translation = PublicBodyCategory::Translation.new(attrs)
+ translations << new_translation
+ end
+ end
+ end
+
private
def self.load_categories()
I18n.available_locales.each do |locale|
diff --git a/app/models/public_body_heading.rb b/app/models/public_body_heading.rb
index 74f7a4374..6c7d645da 100644
--- a/app/models/public_body_heading.rb
+++ b/app/models/public_body_heading.rb
@@ -11,5 +11,37 @@ class PublicBodyHeading < ActiveRecord::Base
translates :name
- validates_uniqueness_of :name, :message => N_("Name is already taken")
+ validates_uniqueness_of :name, :message => N_('Name is already taken')
+ validates_presence_of :name, :message => N_('Name can\'t be blank')
+
+ # Convenience methods for creating/editing translations via forms
+ def find_translation_by_locale(locale)
+ self.translations.find_by_locale(locale)
+ end
+
+ def translated_versions
+ translations
+ end
+
+ def translated_versions=(translation_attrs)
+ def skip?(attrs)
+ valueless = attrs.inject({}) { |h, (k, v)| h[k] = v if v != '' and k != 'locale'; h } # because we want to fall back to alternative translations where there are empty values
+ return valueless.length == 0
+ end
+
+ if translation_attrs.respond_to? :each_value # Hash => updating
+ translation_attrs.each_value do |attrs|
+ next if skip?(attrs)
+ t = translation_for(attrs[:locale]) || PublicBodyHeading::Translation.new
+ t.attributes = attrs
+ t.save!
+ end
+ else # Array => creating
+ translation_attrs.each do |attrs|
+ next if skip?(attrs)
+ new_translation = PublicBodyHeading::Translation.new(attrs)
+ translations << new_translation
+ end
+ end
+ end
end
diff --git a/app/views/admin_general/_admin_navbar.html.erb b/app/views/admin_general/_admin_navbar.html.erb
index 5cc740f70..dcf55dc50 100644
--- a/app/views/admin_general/_admin_navbar.html.erb
+++ b/app/views/admin_general/_admin_navbar.html.erb
@@ -10,6 +10,7 @@
<li><%= link_to 'Stats', admin_stats_path %></li>
<li><%= link_to 'Debug', admin_debug_path %></li>
<li><%= link_to 'Authorities', admin_body_list_path %></li>
+ <li><%= link_to 'Categories', admin_category_index_path %></li>
<li><%= link_to 'Requests', admin_request_list_path %></li>
<li><%= link_to 'Users', admin_user_list_path %></li>
<li><%= link_to 'Tracks', admin_track_list_path %></li>
diff --git a/app/views/admin_public_body_category/_form.html.erb b/app/views/admin_public_body_category/_form.html.erb
new file mode 100644
index 000000000..91f6285de
--- /dev/null
+++ b/app/views/admin_public_body_category/_form.html.erb
@@ -0,0 +1,55 @@
+<%= error_messages_for 'category' %>
+
+<!--[form:public_body_category]-->
+
+<div id="div-locales">
+ <ul class="locales nav nav-tabs">
+ <% I18n.available_locales.each_with_index do |locale, i| %>
+ <li><a href="#div-locale-<%=locale.to_s%>" data-toggle="tab" ><%=locale_name(locale.to_s) || "Default locale"%></a></li>
+ <% end %>
+ </ul>
+ <div class="tab-content">
+<%
+ I18n.available_locales.each do |locale|
+ if locale==I18n.default_locale # The default locale is submitted as part of the bigger object...
+ prefix = 'public_body_category'
+ object = @category
+ else # ...but additional locales go "on the side"
+ prefix = "public_body_category[translated_versions][]"
+ object = @category.new_record? ?
+ PublicBodyCategory::Translation.new :
+ @category.find_translation_by_locale(locale.to_s) || PublicBodyCategory::Translation.new
+ end
+%>
+ <%= fields_for prefix, object do |t| %>
+ <div class="tab-pane" id="div-locale-<%=locale.to_s%>">
+ <div class="control-group">
+ <%= t.hidden_field :locale, :value => locale.to_s %>
+ <label for="<%= form_tag_id(t.object_name, :title, locale) %>" class="control-label">Title</label>
+ <div class="controls">
+ <%= t.text_field :title, :id => form_tag_id(t.object_name, :title, locale), :class => "span4" %>
+ </div>
+ </div>
+ <div class="control-group">
+ <label for="<%= form_tag_id(t.object_name, :description, locale) %>" class="control-label">Description</label>
+ <div class="controls">
+ <%= t.text_field :description, :id => form_tag_id(t.object_name, :description, locale), :class => "span4" %>
+ </div>
+ </div>
+ </div>
+ <%
+ end
+end
+%>
+ </div>
+</div>
+
+<h3>Common Fields</h3>
+
+<div class="control-group">
+ <label for="public_body_category_category_tag" class="control-label">Category tag</label>
+ <div class="controls">
+ <%= f.text_field :category_tag, :class => "span4" %>
+ </div>
+</div>
+<!--[eoform:public_body_category]-->
diff --git a/app/views/admin_public_body_category/_one_list.html.erb b/app/views/admin_public_body_category/_one_list.html.erb
new file mode 100644
index 000000000..7303ff803
--- /dev/null
+++ b/app/views/admin_public_body_category/_one_list.html.erb
@@ -0,0 +1,43 @@
+<div class="accordion" id="categories">
+ <% for heading in category_headings %>
+ <h3>
+ <%= link_to(heading.name, admin_heading_edit_path(heading), :title => "view full details")%>
+ </h3>
+ <div>
+ <% for category in heading.public_body_categories %>
+ <div class="accordion-group">
+ <div class="accordion-heading accordion-toggle row">
+ <span class="item-title span6">
+ <a href="#category_<%=category.id%>" data-toggle="collapse" data-parent="requests"><%= chevron_right %></a>
+
+ <%= link_to(category.title, admin_category_edit_path(category), :title => "view full details")%>
+ </span>
+ <span class="item-metadata span6">
+ <span class="label label-info tag">
+ <%= category.category_tag %>
+ </span>
+ </span>
+ </div>
+ <div id="category_<%=category.id%>" class="item-detail accordion-body collapse row">
+ <div>
+ <span class="span6">
+ <b>name</b>
+ </span>
+ <span class="span6">
+ <%= h category.title %>&nbsp;
+ </span>
+ </div>
+ <div>
+ <span class="span6">
+ <b>description</b>
+ </span>
+ <span class="span6">
+ <%= h category.description %>&nbsp;
+ </span>
+ </div>
+ </div>
+ <% end %>
+ </div>
+ </div>
+ <% end %>
+</div> \ No newline at end of file
diff --git a/app/views/admin_public_body_category/edit.html.erb b/app/views/admin_public_body_category/edit.html.erb
new file mode 100644
index 000000000..c1e522cac
--- /dev/null
+++ b/app/views/admin_public_body_category/edit.html.erb
@@ -0,0 +1,30 @@
+<h1><%=@title%></h1>
+
+<div class="row">
+ <div class="span8">
+ <div id="public_body_category_form">
+ <%= form_for @category, :url => admin_category_update_path(@category), :html => { :class => "form form-horizontal" } do |f| %>
+ <%= render :partial => 'form', :locals => {:f => f} %>
+ <div class="form-actions">
+ <%= f.submit 'Save', :accesskey => 's', :class => "btn btn-success" %></p>
+ </div>
+ <% end %>
+ </div>
+</div>
+
+<div class="row">
+ <div class="span8 well">
+ <%= link_to 'List all', admin_category_index_path, :class => "btn" %>
+ </div>
+</div>
+
+<% if @tagged_public_bodies.empty? %>
+ <div class="row">
+ <div class="span8">
+ <%= form_tag(admin_category_destroy_path(@category), :class => "form form-inline") do %>
+ <%= hidden_field_tag(:public_body_id, { :value => @category.id } ) %>
+ <%= submit_tag "Destroy #{@category.title}", :title => @category.title, :class => "btn btn-danger" %> (this is permanent!)
+ <% end %>
+ </div>
+ </div>
+<% end %> \ No newline at end of file
diff --git a/app/views/admin_public_body_category/index.html.erb b/app/views/admin_public_body_category/index.html.erb
new file mode 100644
index 000000000..ca3a4b0ff
--- /dev/null
+++ b/app/views/admin_public_body_category/index.html.erb
@@ -0,0 +1,16 @@
+<% @title = 'Listing public authority categories' %>
+
+<h1><%=@title%></h1>
+
+<div class="btn-toolbar">
+ <div class="btn-group">
+ <%= link_to 'New category', admin_category_new_path, :class => "btn btn-primary" %>
+ </div>
+ <div class="btn-group">
+ <%= link_to 'New category heading', admin_heading_new_path, :class => "btn" %>
+ </div>
+</div>
+
+<h2>All categories</h2>
+
+<%= render :partial => 'one_list', :locals => { :category_headings => @category_headings, :table_name => 'exact' } %>
diff --git a/app/views/admin_public_body_category/new.html.erb b/app/views/admin_public_body_category/new.html.erb
new file mode 100644
index 000000000..0a016182f
--- /dev/null
+++ b/app/views/admin_public_body_category/new.html.erb
@@ -0,0 +1,21 @@
+<% @title = 'New category' %>
+
+<h1><%=@title%></h1>
+<div class="row">
+ <div class="span8">
+ <div id="public_category_form">
+ <%= form_for @category, :as => :public_body_category, :url => admin_category_create_path, :html => {:class => "form form-horizontal"} do |f| %>
+ <%= render :partial => 'form', :locals => {:f => f} %>
+
+ <div class="form-actions">
+ <%= f.submit "Create", :class => "btn btn-primary" %>
+ </div>
+ <% end %>
+ <div class="row">
+ <div class="span8 well">
+ <%= link_to 'List all', admin_category_index_path, :class => "btn" %>
+ </div>
+ </div>
+ </div>
+ </div>
+</div>
diff --git a/app/views/admin_public_body_heading/_form.html.erb b/app/views/admin_public_body_heading/_form.html.erb
new file mode 100644
index 000000000..d4e914ca1
--- /dev/null
+++ b/app/views/admin_public_body_heading/_form.html.erb
@@ -0,0 +1,41 @@
+<%= error_messages_for 'heading' %>
+
+<!--[form:public_body_heading]-->
+
+<div id="div-locales">
+ <ul class="locales nav nav-tabs">
+ <% I18n.available_locales.each_with_index do |locale, i| %>
+ <li><a href="#div-locale-<%=locale.to_s%>" data-toggle="tab" ><%=locale_name(locale.to_s) || "Default locale"%></a></li>
+ <% end %>
+ </ul>
+ <div class="tab-content">
+<%
+ for locale in I18n.available_locales do
+ if locale==I18n.default_locale # The default locale is submitted as part of the bigger object...
+ prefix = 'public_body_heading'
+ object = @heading
+ else # ...but additional locales go "on the side"
+ prefix = "public_body_heading[translated_versions][]"
+ object = @heading.new_record? ?
+ PublicBodyHeading::Translation.new :
+ @heading.find_translation_by_locale(locale.to_s) || PublicBodyHeading::Translation.new
+ end
+%>
+ <%= fields_for prefix, object do |t| %>
+ <div class="tab-pane" id="div-locale-<%=locale.to_s%>">
+ <div class="control-group">
+ <%= t.hidden_field :locale, :value => locale.to_s %>
+ <label for="<%= form_tag_id(t.object_name, :name, locale) %>" class="control-label">Name</label>
+ <div class="controls">
+ <%= t.text_field :name, :id => form_tag_id(t.object_name, :name, locale), :class => "span4" %>
+ </div>
+ </div>
+ </div>
+ <%
+ end
+end
+%>
+ </div>
+</div>
+
+<!--[eoform:public_body_heading]-->
diff --git a/app/views/admin_public_body_heading/edit.html.erb b/app/views/admin_public_body_heading/edit.html.erb
new file mode 100644
index 000000000..df5ae7d0e
--- /dev/null
+++ b/app/views/admin_public_body_heading/edit.html.erb
@@ -0,0 +1,30 @@
+<h1><%=@title%></h1>
+
+<div class="row">
+ <div class="span8">
+ <div id="public_body_heading_form">
+ <%= form_for @heading, :url => admin_heading_update_path(@heading), :html => { :class => "form form-horizontal" } do |f| %>
+ <%= render :partial => 'form', :locals => {:f => f} %>
+ <div class="form-actions">
+ <%= f.submit 'Save', :accesskey => 's', :class => "btn btn-success" %></p>
+ </div>
+ <% end %>
+ </div>
+</div>
+
+<div class="row">
+ <div class="span8 well">
+ <%= link_to 'List all', admin_category_index_path, :class => "btn" %>
+ </div>
+</div>
+
+<% if @heading.public_body_categories.empty? %>
+ <div class="row">
+ <div class="span8">
+ <%= form_tag(admin_heading_destroy_path(@heading), :class => "form form-inline") do %>
+ <%= hidden_field_tag(:public_body_heading_id, { :value => @heading.id } ) %>
+ <%= submit_tag "Destroy #{@heading.name}", :name => @heading.name, :class => "btn btn-danger" %> (this is permanent!)
+ <% end %>
+ </div>
+ </div>
+<% end %> \ No newline at end of file
diff --git a/app/views/admin_public_body_heading/new.html.erb b/app/views/admin_public_body_heading/new.html.erb
new file mode 100644
index 000000000..22cb88901
--- /dev/null
+++ b/app/views/admin_public_body_heading/new.html.erb
@@ -0,0 +1,21 @@
+<% @title = 'New category heading' %>
+
+<h1><%=@title%></h1>
+<div class="row">
+ <div class="span8">
+ <div id="public_heading_form">
+ <%= form_for @heading, :as => :public_body_heading, :url => admin_heading_create_path, :html => {:class => "form form-horizontal"} do |f| %>
+ <%= render :partial => 'form', :locals => {:f => f} %>
+
+ <div class="form-actions">
+ <%= f.submit "Create", :class => "btn btn-primary" %>
+ </div>
+ <% end %>
+ <div class="row">
+ <div class="span8 well">
+ <%= link_to 'List all', admin_category_index_path, :class => "btn" %>
+ </div>
+ </div>
+ </div>
+ </div>
+</div>