From: Matt Amos Date: Sat, 31 Mar 2012 22:44:18 +0000 (+0100) Subject: Added redactions resource X-Git-Tag: live~5598 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/851de28303fc9c24d095b9452b862bea618ec0da Added redactions resource --- diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7043d8206..d98747253 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -111,6 +111,20 @@ class ApplicationController < ActionController::Base require_capability(:allow_write_gpx) end + ## + # require that the user is a moderator, or fill out a helpful error message + # and return them to the blocks index. + def require_moderator + unless @user.moderator? + if request.get? + flash[:error] = t('application.require_moderator.not_a_moderator') + redirect_to :action => 'index' + else + render :nothing => true, :status => :forbidden + end + end + end + ## # sets up the @user object for use by other methods. this is mostly called # from the authorize method, but can be called elsewhere if authorisation diff --git a/app/controllers/redactions_controller.rb b/app/controllers/redactions_controller.rb new file mode 100644 index 000000000..201f1883f --- /dev/null +++ b/app/controllers/redactions_controller.rb @@ -0,0 +1,84 @@ +class RedactionsController < ApplicationController + layout 'site' + + before_filter :authorize_web + before_filter :set_locale + before_filter :require_user, :only => [:new, :create, :edit, :update, :destroy] + before_filter :require_moderator, :only => [:new, :create, :edit, :update, :destroy] + before_filter :lookup_redaction, :only => [:show, :edit, :update, :destroy] + before_filter :check_database_readable + before_filter :check_database_writable, :only => [:create, :update, :destroy] + + def index + @redactions_pages, @redactions = paginate(:redactions, :order => :id, :per_page => 10) + end + + def new + @redaction = Redaction.new + end + + def create + @redaction = Redaction.new + @redaction.user_id = @user.id + @redaction.title = params[:redaction][:title] + @redaction.description = params[:redaction][:description] + # didn't see this come in from the form - maybe i'm doing something + # wrong, or markdown is the only thing supported at the moment? + @redaction.description_format = 'markdown' + + if @redaction.save + flash[:notice] = t('redaction.create.flash') + redirect_to @redaction + else + render :action => 'new' + end + end + + def show + end + + def edit + end + + def update + # note - don't update the user ID + + if params[:redaction][:title] and params[:redaction][:title] != @redaction.title + @redaction.title = params[:redaction][:title] + end + + if params[:redaction][:description] and params[:redaction][:description] != @redaction.description + @redaction.description = params[:redaction][:description] + end + + if @redaction.save + flash[:notice] = t('redaction.update.flash') + redirect_to @redaction + else + render :action => 'edit' + end + end + + def destroy + unless @redaction.old_nodes.empty? and + @redaction.old_ways.empty? and + @redaction.old_relations.empty? + flash[:error] = t('redaction.destroy.not_empty') + redirect_to @redaction + else + if @redaction.destroy + flash[:notice] = t('redaction.destroy.flash') + redirect_to :index + else + flash[:error] = t('redaction.destroy.error') + redirect_to @redaction + end + end + end + + private + + def lookup_redaction + @redaction = Redaction.find(params[:id]) + end +end diff --git a/app/controllers/user_blocks_controller.rb b/app/controllers/user_blocks_controller.rb index 103909e38..455e45c3f 100644 --- a/app/controllers/user_blocks_controller.rb +++ b/app/controllers/user_blocks_controller.rb @@ -106,20 +106,6 @@ class UserBlocksController < ApplicationController end private - ## - # require that the user is a moderator, or fill out a helpful error message - # and return them to the blocks index. - def require_moderator - unless @user.moderator? - if request.get? - flash[:error] = t('user_block.filter.not_a_moderator') - redirect_to :action => 'index' - else - render :nothing => true, :status => :forbidden - end - end - end - ## # ensure that there is a "user_block" instance variable def lookup_user_block diff --git a/app/models/redaction.rb b/app/models/redaction.rb index 28a451242..ca0ea7232 100644 --- a/app/models/redaction.rb +++ b/app/models/redaction.rb @@ -8,7 +8,13 @@ # displayed linked from the redacted records. # class Redaction < ActiveRecord::Base - has_many :nodes - has_many :ways - has_many :relations + belongs_to :user + + has_many :old_nodes + has_many :old_ways + has_many :old_relations + + def description + RichText.new(read_attribute(:description_format), read_attribute(:description)) + end end diff --git a/app/views/redactions/_redaction.html.erb b/app/views/redactions/_redaction.html.erb new file mode 100644 index 000000000..c246004b8 --- /dev/null +++ b/app/views/redactions/_redaction.html.erb @@ -0,0 +1 @@ +
  • <%= link_to h(redaction.title), :controller => 'redaction', :action => 'show', :id => redaction.id %>
  • diff --git a/app/views/redactions/_redactions.html.erb b/app/views/redactions/_redactions.html.erb new file mode 100644 index 000000000..a6b6820ed --- /dev/null +++ b/app/views/redactions/_redactions.html.erb @@ -0,0 +1,3 @@ + diff --git a/app/views/redactions/edit.html.erb b/app/views/redactions/edit.html.erb new file mode 100644 index 000000000..2a5e69d0a --- /dev/null +++ b/app/views/redactions/edit.html.erb @@ -0,0 +1,19 @@ +<% @title = t 'redaction.edit.title' %> +

    <%= t 'redaction.edit.heading' %>

    + +<%= form_for(@redaction) do |f| %> + <%= f.error_messages %> + +

    + <%= f.label :title, t('redaction.edit.title') %>
    + <%= f.text_field :title %> +

    +

    + <%= f.label :description, t('redaction.edit.description') %>
    + <%= richtext_area :redaction, :description, :cols => 80, :rows => 20, :format => @redaction.description_format %> +

    +

    + <%= f.submit t('redaction.edit.submit') %> +

    +<% end %> + diff --git a/app/views/redactions/index.html.erb b/app/views/redactions/index.html.erb new file mode 100644 index 000000000..1fa683de8 --- /dev/null +++ b/app/views/redactions/index.html.erb @@ -0,0 +1,8 @@ +<% @title = t('redaction.index.title') %> +

    <%= t('redaction.index.heading') %>

    + +<% unless @redactions.empty? %> + <%= render :partial => 'redactions' %> +<% else %> +

    <%= t 'redaction.index.empty' %>

    +<% end %> diff --git a/app/views/redactions/new.html.erb b/app/views/redactions/new.html.erb new file mode 100644 index 000000000..c714acdbc --- /dev/null +++ b/app/views/redactions/new.html.erb @@ -0,0 +1,20 @@ +<% @title = t 'redaction.new.title' %> +

    <%= t 'redaction.new.heading' %>

    + +<%= form_for(@redaction) do |f| %> + <%= f.error_messages %> + +

    + <%= f.label :title, t('redaction.new.title') %>
    + <%= f.text_field :title %> +

    +

    + <%= f.label :description, t('redaction.new.description') %>
    + <%= richtext_area :redaction, :description, :cols => 80, :rows => 20, :format => @redaction.description_format %> +

    +

    + <%= f.submit t('redaction.new.submit') %> +

    +<% end %> + + diff --git a/app/views/redactions/show.html.erb b/app/views/redactions/show.html.erb new file mode 100644 index 000000000..04df9ade9 --- /dev/null +++ b/app/views/redactions/show.html.erb @@ -0,0 +1,16 @@ +<% @title = t('redaction.show.title') %> +

    <%= t('redaction.show.heading', :title => @redaction.title) %>

    + +

    + <%= t 'redaction.show.user' %> + <%= link_to(h(@redaction.user.display_name), {:controller => 'user', :action => 'view', :display_name => @redaction.user.display_name}) %> +

    +

    + <%= t 'redaction.show.description' %> + <%= @redaction.description.to_html %> +

    + +<% if @user and @user.moderator? %> +<%= link_to t('redaction.show.edit'), edit_redaction_path(@redaction) %> +<%= button_to(t('redaction.show.destroy'), @redaction, :confirm => t('redaction.show.confirm'), :method => "delete", :remote => true) %> +<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 3a9a62978..dce8e3ead 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1522,6 +1522,8 @@ en: application: require_cookies: cookies_needed: "You appear to have cookies disabled - please enable cookies in your browser before continuing." + require_moderator: + not_a_moderator: "You need to be a moderator to perform that action." setup_user_auth: blocked: "Your access to the API has been blocked. Please log-in to the web interface to find out more." need_to_see_terms: "Your access to the API is temporarily suspended. Please log-in to the web interface to view the Contributor Terms. You do not need to agree, but you must view them." @@ -1921,7 +1923,6 @@ en: back: "View all blocks" needs_view: "Does the user need to log in before this block will be cleared?" filter: - not_a_moderator: "You need to be a moderator to perform that action." block_expired: "The block has already expired and cannot be edited." block_period: "The blocking period must be one of the values selectable in the drop-down list." create: @@ -1999,3 +2000,34 @@ en: history_tooltip: View edits for this area history_disabled_tooltip: Zoom in to view edits for this area history_zoom_alert: You must zoom in to view edits for this area + redaction: + edit: + description: "Description" + heading: "Edit redaction" + submit: "Save redaction" + title: "Edit redaction" + index: + empty: "No redactions to show." + heading: "List of redactions" + title: "List of redactions" + new: + description: "Description" + heading: "Enter information for new redaction" + submit: "Create redaction" + title: "Creating new redaction" + show: + description: "Description" + heading: "Showing redaction \"%{title}\"" + title: "Showing redaction" + user: "Creator:" + edit: "Edit this redaction." + destroy: "Remove this redaction" + confirm: "Are you sure?" + create: + flash: "Redaction created." + update: + flash: "Changes saved." + destroy: + not_empty: "Redaction is not empty. Please un-redact all versions belonging to this redaction before destroying it." + flash: "Redaction destroyed." + error: "There was an error destroying this redaction." diff --git a/config/routes.rb b/config/routes.rb index 73540ca7f..bc4c12c97 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -229,4 +229,7 @@ OpenStreetMap::Application.routes.draw do match '/blocks/new/:display_name' => 'user_blocks#new', :via => :get, :as => "new_user_block" resources :user_blocks match '/blocks/:id/revoke' => 'user_blocks#revoke', :via => [:get, :post], :as => "revoke_user_block" + + # redactions + resources :redactions end diff --git a/db/migrate/20120318201948_create_redactions.rb b/db/migrate/20120318201948_create_redactions.rb index bcb3929cb..b6ee96037 100644 --- a/db/migrate/20120318201948_create_redactions.rb +++ b/db/migrate/20120318201948_create_redactions.rb @@ -20,7 +20,7 @@ class CreateRedactions < ActiveRecord::Migration remove_foreign_key tbl, [:redaction_id], :redactions, [:id] remove_column tbl, :redaction_id end - + drop_table :redactions end end diff --git a/db/migrate/20120404205604_add_user_and_description_to_redaction.rb b/db/migrate/20120404205604_add_user_and_description_to_redaction.rb new file mode 100644 index 000000000..05c75d37e --- /dev/null +++ b/db/migrate/20120404205604_add_user_and_description_to_redaction.rb @@ -0,0 +1,17 @@ +require 'migrate' + +class AddUserAndDescriptionToRedaction < ActiveRecord::Migration + def up + add_column :redactions, :user_id, :bigint, :null => false + add_column :redactions, :description_format, :format_enum, :null => false, :default => "markdown" + + add_foreign_key :redactions, [:user_id], :users, [:id] + end + + def down + remove_foreign_key :redactions, [:user_id], :users, [:id] + + remove_column :redactions, :description_format + remove_column :redactions, :user_id + end +end