diff --git a/app/assets/javascripts/artist_commentaries.js b/app/assets/javascripts/artist_commentaries.js new file mode 100644 index 000000000..e36919706 --- /dev/null +++ b/app/assets/javascripts/artist_commentaries.js @@ -0,0 +1,62 @@ +(function() { + Danbooru.ArtistCommentary = {}; + + Danbooru.ArtistCommentary.initialize_all = function() { + if ($("#c-posts").length && $("#a-show").length) { + if ($("#original-artist-commentary").length && $("#translated-artist-commentary").length) { + this.initialize_commentary_display_tabs(); + } + + this.initialize_edit_commentary_dialog(); + } + } + + Danbooru.ArtistCommentary.initialize_commentary_display_tabs = function() { + $("#commentary-sections li a").click(function(e) { + if (e.target.hash === "#original") { + $("#original-artist-commentary").show(); + $("#translated-artist-commentary").hide(); + } else if (e.target.hash === "#translated") { + $("#original-artist-commentary").hide(); + $("#translated-artist-commentary").show(); + } + + $("#commentary-sections li").removeClass("active"); + $(e.target).parent("li").addClass("active"); + e.preventDefault(); + }); + + $("#commentary-sections li:last-child").addClass("active"); + $("#original-artist-commentary").hide(); + } + + Danbooru.ArtistCommentary.initialize_edit_commentary_dialog = function() { + $("#add-commentary-dialog").dialog({ + autoOpen: false, + width: 500, + modal: true, + buttons: { + "Submit": function() { + $("#add-commentary-dialog form").submit(); + $(this).dialog("close"); + }, + "Cancel": function() { + $(this).dialog("close"); + } + } + }); + + $('#add-commentary-dialog form').submit(function() { + $('#add-commentary-dialog').dialog('close'); + }); + + $("#add-commentary").click(function(e) { + e.preventDefault(); + $("#add-commentary-dialog").dialog("open"); + }); + } +})(); + +$(function() { + Danbooru.ArtistCommentary.initialize_all(); +}); diff --git a/app/assets/stylesheets/specific/posts.css.scss b/app/assets/stylesheets/specific/posts.css.scss index 893a336fa..70324be53 100644 --- a/app/assets/stylesheets/specific/posts.css.scss +++ b/app/assets/stylesheets/specific/posts.css.scss @@ -450,3 +450,14 @@ div#unapprove-dialog { font-size: 0.9em; } } + +#add-commentary-dialog { + input { + width: 70%; + } + + textarea { + font-size: 1em; + width: 100%; + } +} diff --git a/app/controllers/artist_commentaries_controller.rb b/app/controllers/artist_commentaries_controller.rb new file mode 100644 index 000000000..59fb1feff --- /dev/null +++ b/app/controllers/artist_commentaries_controller.rb @@ -0,0 +1,23 @@ +class ArtistCommentariesController < ApplicationController + respond_to :html, :xml, :json, :js + before_filter :member_only + + def create_or_update + @artist_commentary = ArtistCommentary.find_by_post_id(params[:artist_commentary][:post_id]) + + if @artist_commentary + @artist_commentary.update_attributes(params[:artist_commentary]) + else + @artist_commentary = ArtistCommentary.create(params[:artist_commentary]) + end + + respond_with(@artist_commentary.post) + end + + def revert + @artist_commentary = ArtistCommentary.find_by_post_id(params[:id]) + @version = ArtistCommentaryVersion.find(params[:version_id]) + @artist_commentary.revert_to!(@version) + respond_with(@artist_commentary) + end +end diff --git a/app/controllers/artist_commentary_versions_controller.rb b/app/controllers/artist_commentary_versions_controller.rb new file mode 100644 index 000000000..305c6f224 --- /dev/null +++ b/app/controllers/artist_commentary_versions_controller.rb @@ -0,0 +1,12 @@ +class ArtistCommentaryVersionsController < ApplicationController + respond_to :html, :xml, :json + + def index + @commentary_versions = ArtistCommentaryVersion.search(params[:search]).order("artist_commentary_versions.id desc").paginate(params[:page], :limit => params[:limit]) + respond_with(@commentary_versions) do |format| + format.xml do + render :xml => @commentary_versions.to_xml(:root => "artist-commentary-versions") + end + end + end +end diff --git a/app/models/artist_commentary.rb b/app/models/artist_commentary.rb new file mode 100644 index 000000000..8a769e626 --- /dev/null +++ b/app/models/artist_commentary.rb @@ -0,0 +1,41 @@ +class ArtistCommentary < ActiveRecord::Base + attr_accessible :post_id, :original_description, :original_title, :translated_description, :translated_title + validates_uniqueness_of :post_id + belongs_to :post + has_many :versions, :class_name => "ArtistCommentaryVersion", :foreign_key => :post_id, :primary_key => :post_id, :order => "artist_commentary_versions.id ASC" + after_save :create_version + + def original_present? + original_title.present? || original_description.present? + end + + def translated_present? + translated_title.present? || translated_description.present? + end + + def any_field_present? + original_present? || translated_present? + end + + def create_version + versions.create( + :post_id => post_id, + :original_title => original_title, + :original_description => original_description, + :translated_title => translated_title, + :translated_description => translated_description + ) + end + + def revert_to(version) + self.original_description = version.original_description + self.original_title = version.original_title + self.translated_description = version.translated_description + self.translated_title = version.translated_title + end + + def revert_to!(version) + revert_to(version) + save! + end +end diff --git a/app/models/artist_commentary_version.rb b/app/models/artist_commentary_version.rb new file mode 100644 index 000000000..a6c2e23be --- /dev/null +++ b/app/models/artist_commentary_version.rb @@ -0,0 +1,29 @@ +class ArtistCommentaryVersion < ActiveRecord::Base + before_validation :initialize_updater + belongs_to :updater, :class_name => "User" + scope :for_user, lambda {|user_id| where("updater_id = ?", user_id)} + + def self.search(params) + q = scoped + params = {} if params.blank? + + if params[:updater_id] + q = q.where("updater_id = ?", params[:updater_id].to_i) + end + + if params[:post_id] + q = q.where("post_id = ?", params[:post_id].to_i) + end + + q + end + + def initialize_updater + self.updater_id = CurrentUser.id + self.updater_ip_addr = CurrentUser.ip_addr + end + + def updater_name + User.id_to_name(updater_id) + end +end diff --git a/app/models/post.rb b/app/models/post.rb index 1649d5af2..8c83d6517 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -23,6 +23,7 @@ class Post < ActiveRecord::Base belongs_to :uploader, :class_name => "User" belongs_to :parent, :class_name => "Post" has_one :upload, :dependent => :destroy + has_one :artist_commentary has_many :flags, :class_name => "PostFlag", :dependent => :destroy has_many :appeals, :class_name => "PostAppeal", :dependent => :destroy has_many :versions, :class_name => "PostVersion", :dependent => :destroy, :order => "post_versions.id ASC" diff --git a/app/views/artist_commentaries/_form.html.erb b/app/views/artist_commentaries/_form.html.erb new file mode 100644 index 000000000..dc3ee1f47 --- /dev/null +++ b/app/views/artist_commentaries/_form.html.erb @@ -0,0 +1,19 @@ +
If the artist of this image posted some interesting additional information about this work, you can copy it here.
+ +<%= form_tag(create_or_update_artist_commentaries_path, :class => "simple_form", :method => :put) do %> +<%= format_text artist_commentary.original_description %>
+<%= format_text artist_commentary.translated_description %>
+| Post | +Original | +Translated | + <% if CurrentUser.is_janitor? %> +IP Address | + <% end %> +Edited By | +Date | + <% if CurrentUser.is_member? %> ++ <% end %> + |
|---|---|---|---|---|---|---|
| <%= link_to commentary_version.post_id, post_path(commentary_version.post_id) %> | +
+ <%= h(commentary_version.original_title) %>+ <%= h(commentary_version.original_description) %> + |
+
+ <%= h(commentary_version.translated_title) %>+ <%= h(commentary_version.translated_description) %> + |
+ <% if CurrentUser.is_janitor? %>
+ + <%= commentary_version.updater_ip_addr %> + | + <% end %> +<%= link_to_user commentary_version.updater %> | +<%= compact_time commentary_version.updated_at %> | + <% if CurrentUser.is_member? %> ++ <%= link_to "Revert to", revert_artist_commentary_path(commentary_version.post_id, :version_id => commentary_version.id), :remote => true, :method => :put, :confirm => "Are you sure you want to revert to this version?" %> + | + <% end %> +