sync
This commit is contained in:
@@ -1,6 +1,16 @@
|
||||
class Advertisement < ActiveRecord::Base
|
||||
validates_inclusion_of :ad_type, :in => %w(horizontal vertical)
|
||||
has_many :hits, :class_name => "AdvertisementHit"
|
||||
after_create :copy_to_servers
|
||||
after_destroy :delete_from_servers
|
||||
|
||||
def copy_to_servers
|
||||
RemoteServer.copy_to_all(image_path, image_path)
|
||||
end
|
||||
|
||||
def delete_from_servers
|
||||
RemoteServer.delete_from_all(image_path)
|
||||
end
|
||||
|
||||
def hit!(ip_addr)
|
||||
hits.create(:ip_addr => ip_addr)
|
||||
@@ -10,23 +20,21 @@ class Advertisement < ActiveRecord::Base
|
||||
hits.where(["created_at BETWEEN ? AND ?", start_date, end_date]).count
|
||||
end
|
||||
|
||||
def date_path
|
||||
created_at.strftime("%Y%m%d")
|
||||
def unique_identifier
|
||||
@unique_identifier ||= ("%.0f" % (Time.now.to_f * 1_000))
|
||||
end
|
||||
|
||||
def image_url
|
||||
"/images/ads-#{date_path}/#{file_name}"
|
||||
"/images/advertisements/#{file_name}"
|
||||
end
|
||||
|
||||
def image_path
|
||||
"#{Rails.root}/public/#{image_url}"
|
||||
"#{Rails.root}/public/images/advertisements/#{file_name}"
|
||||
end
|
||||
|
||||
def file=(f)
|
||||
if f.size > 0
|
||||
self.created_at ||= Time.now
|
||||
self.file_name = f.original_filename
|
||||
FileUtils.mkdir_p(File.dirname(image_path))
|
||||
self.file_name = unique_identifier + File.extname(f.original_filename)
|
||||
|
||||
if f.local_path
|
||||
FileUtils.cp(f.local_path, image_path)
|
||||
@@ -35,7 +43,6 @@ class Advertisement < ActiveRecord::Base
|
||||
end
|
||||
|
||||
File.chmod(0644, image_path)
|
||||
|
||||
image_size = ImageSize.new(File.open(image_path, "rb"))
|
||||
self.width = image_size.get_width
|
||||
self.height = image_size.get_height
|
||||
@@ -43,23 +50,23 @@ class Advertisement < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def preview_width
|
||||
if width > 200 || height > 200
|
||||
if width > 100 || height > 100
|
||||
if width < height
|
||||
ratio = 200.0 / height
|
||||
ratio = 100.0 / height
|
||||
return (width * ratio).to_i
|
||||
else
|
||||
return 200
|
||||
return 100
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def preview_height
|
||||
if width > 200 || height > 200
|
||||
if width > 100 || height > 100
|
||||
if height < width
|
||||
ratio = 200.0 / width
|
||||
ratio = 100.0 / width
|
||||
return (height * ratio)
|
||||
else
|
||||
return 200
|
||||
return 100
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
class AdvertisementHit < ActiveRecord::Base
|
||||
belongs_to :advertisement
|
||||
|
||||
scope :between, lambda {|start_date, end_date| where("created_at BETWEEN ? AND ?", start_date, end_date)}
|
||||
end
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
class Artist < ActiveRecord::Base
|
||||
attr_accessor :updater_id, :updater_ip_addr
|
||||
before_create :initialize_creator
|
||||
before_save :normalize_name
|
||||
after_save :create_version
|
||||
after_save :save_url_string
|
||||
validates_uniqueness_of :name
|
||||
validates_presence_of :updater_id, :updater_ip_addr
|
||||
belongs_to :updater, :class_name => "User"
|
||||
belongs_to :creator, :class_name => "User"
|
||||
has_many :members, :class_name => "Artist", :foreign_key => "group_name", :primary_key => "name"
|
||||
has_many :artist_urls, :dependent => :destroy
|
||||
has_many :urls, :dependent => :destroy, :class_name => "ArtistUrl"
|
||||
has_one :wiki_page, :foreign_key => "title", :primary_key => "name"
|
||||
has_one :tag_alias, :foreign_key => "antecedent_name", :primary_key => "name"
|
||||
accepts_nested_attributes_for :wiki_page
|
||||
attr_accessible :name, :url_string, :other_names, :group_name, :wiki_page_attributes, :updater_id, :updater_ip_addr
|
||||
attr_accessible :name, :url_string, :other_names, :group_name, :wiki_page_attributes, :notes
|
||||
|
||||
module UrlMethods
|
||||
module ClassMethods
|
||||
@@ -24,7 +21,7 @@ class Artist < ActiveRecord::Base
|
||||
while artists.empty? && url.size > 10
|
||||
u = url.sub(/\/+$/, "") + "/"
|
||||
u = u.to_escaped_for_sql_like.gsub(/\*/, '%') + '%'
|
||||
artists += Artist.joins(:artist_urls).where(["artists.is_active = TRUE AND artist_urls.normalized_url LIKE ? ESCAPE E'\\\\'", u]).all(:order => "artists.name")
|
||||
artists += Artist.joins(:urls).where(["artists.is_active = TRUE AND artist_urls.normalized_url LIKE ? ESCAPE E'\\\\'", u]).all(:order => "artists.name")
|
||||
url = File.dirname(url) + "/"
|
||||
end
|
||||
|
||||
@@ -38,10 +35,10 @@ class Artist < ActiveRecord::Base
|
||||
|
||||
def save_url_string
|
||||
if @url_string
|
||||
artist_urls.clear
|
||||
urls.clear
|
||||
|
||||
@url_string.scan(/\S+/).each do |url|
|
||||
artist_urls.create(:url => url)
|
||||
urls.create(:url => url)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -51,7 +48,7 @@ class Artist < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def url_string
|
||||
@url_string || artist_urls.map {|x| x.url}.join("\n")
|
||||
@url_string || urls.map {|x| x.url}.join("\n")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -80,13 +77,15 @@ class Artist < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
module UpdaterMethods
|
||||
def updater_name
|
||||
User.id_to_name(updater_id).tr("_", " ")
|
||||
end
|
||||
end
|
||||
|
||||
module SearchMethods
|
||||
def find_by_name_or_id(params)
|
||||
if params[:name]
|
||||
find_by_name(params[:name])
|
||||
else
|
||||
find(params[:id])
|
||||
end
|
||||
end
|
||||
|
||||
def find_by_any_name(name)
|
||||
build_relation(:name => name).first
|
||||
end
|
||||
@@ -120,6 +119,12 @@ class Artist < ActiveRecord::Base
|
||||
relation = relation.where(["id = ?", params[:id]])
|
||||
end
|
||||
|
||||
if params[:order] == "date"
|
||||
relation = relation.order("updated_at DESC")
|
||||
else
|
||||
relation = relation.order("name")
|
||||
end
|
||||
|
||||
relation
|
||||
end
|
||||
end
|
||||
@@ -129,8 +134,8 @@ class Artist < ActiveRecord::Base
|
||||
ArtistVersion.create(
|
||||
:artist_id => id,
|
||||
:name => name,
|
||||
:updater_id => updater_id,
|
||||
:updater_ip_addr => updater_ip_addr,
|
||||
:updater_id => CurrentUser.user.id,
|
||||
:updater_ip_addr => CurrentUser.ip_addr,
|
||||
:url_string => url_string,
|
||||
:is_active => is_active,
|
||||
:other_names => other_names,
|
||||
@@ -138,14 +143,12 @@ class Artist < ActiveRecord::Base
|
||||
)
|
||||
end
|
||||
|
||||
def revert_to!(version, reverter_id, reverter_ip_addr)
|
||||
def revert_to!(version)
|
||||
self.name = version.name
|
||||
self.url_string = version.url_string
|
||||
self.is_active = version.is_active
|
||||
self.other_names = version.other_names
|
||||
self.group_name = version.group_name
|
||||
self.updater_id = reverter_id
|
||||
self.updater_ip_addr = reverter_ip_addr
|
||||
save
|
||||
end
|
||||
end
|
||||
@@ -172,18 +175,47 @@ class Artist < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
module NoteMethods
|
||||
def notes
|
||||
if wiki_page
|
||||
wiki_page.body
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def notes=(msg)
|
||||
if wiki_page.nil?
|
||||
self.wiki_page = WikiPage.new
|
||||
end
|
||||
|
||||
wiki_page.title = name
|
||||
wiki_page.body = msg
|
||||
wiki_page.save
|
||||
end
|
||||
end
|
||||
|
||||
module TagMethods
|
||||
def has_tag_alias?
|
||||
TagAlias.exists?(["antecedent_name = ?", name])
|
||||
end
|
||||
|
||||
def tag_alias_name
|
||||
TagAlias.find_by_antecedent_name(name).consequent_name
|
||||
end
|
||||
end
|
||||
|
||||
include UrlMethods
|
||||
include NameMethods
|
||||
include GroupMethods
|
||||
include UpdaterMethods
|
||||
extend SearchMethods
|
||||
include VersionMethods
|
||||
extend FactoryMethods
|
||||
include NoteMethods
|
||||
include TagMethods
|
||||
|
||||
def initialize_creator
|
||||
if creator.nil?
|
||||
self.creator_id = updater_id
|
||||
end
|
||||
self.creator_id = CurrentUser.user.id
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ class Post < ActiveRecord::Base
|
||||
before_save :create_tags
|
||||
before_save :update_tag_post_counts
|
||||
before_save :set_tag_counts
|
||||
before_validation_on_create :initialize_uploader
|
||||
belongs_to :updater, :class_name => "User"
|
||||
belongs_to :approver, :class_name => "User"
|
||||
belongs_to :parent, :class_name => "Post"
|
||||
@@ -23,6 +24,7 @@ class Post < ActiveRecord::Base
|
||||
validates_presence_of :parent, :if => lambda {|rec| !rec.parent_id.nil?}
|
||||
validate :validate_parent_does_not_have_a_parent
|
||||
attr_accessible :source, :rating, :tag_string, :old_tag_string, :last_noted_at
|
||||
scope :visible, lambda {|user| Danbooru.config.can_user_see_post_conditions(user)}
|
||||
|
||||
module FileMethods
|
||||
def delete_files
|
||||
@@ -337,6 +339,10 @@ class Post < ActiveRecord::Base
|
||||
def filter_metatags(tags)
|
||||
tags.reject {|tag| tag =~ /\A(?:pool|rating|fav|approver|uploader):/}
|
||||
end
|
||||
|
||||
def has_tag?(tag)
|
||||
tag_string =~ /(?:^| )#{tag}(?:$| )/
|
||||
end
|
||||
end
|
||||
|
||||
module FavoriteMethods
|
||||
@@ -581,6 +587,11 @@ class Post < ActiveRecord::Base
|
||||
end
|
||||
|
||||
module UploaderMethods
|
||||
def initialize_uploader
|
||||
self.uploader = CurrentUser.user
|
||||
self.uploader_ip_addr = CurrentUser.ip_addr
|
||||
end
|
||||
|
||||
def uploader_id=(user_id)
|
||||
self.uploader = User.find(user_id)
|
||||
end
|
||||
@@ -758,6 +769,10 @@ class Post < ActiveRecord::Base
|
||||
tag_array.each {|x| expire_cache(x)}
|
||||
end
|
||||
end
|
||||
|
||||
def is_removed?
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
include FileMethods
|
||||
|
||||
@@ -20,6 +20,10 @@ class RemovedPost < ActiveRecord::Base
|
||||
end
|
||||
count
|
||||
end
|
||||
|
||||
def is_removed?
|
||||
true
|
||||
end
|
||||
|
||||
include Post::FileMethods
|
||||
include Post::ImageMethods
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
class TagAlias < ActiveRecord::Base
|
||||
attr_accessor :creator_ip_addr
|
||||
after_save :update_posts
|
||||
after_destroy :clear_cache
|
||||
after_commit :clear_cache
|
||||
after_commit :clear_remote_cache
|
||||
validates_presence_of :creator_id, :creator_ip_addr
|
||||
validates_uniqueness_of :antecedent_name
|
||||
validate :absence_of_transitive_relation
|
||||
@@ -32,6 +33,12 @@ class TagAlias < ActiveRecord::Base
|
||||
Cache.delete("ta:#{Cache.sanitize(antecedent_name)}")
|
||||
end
|
||||
|
||||
def clear_remote_cache
|
||||
Danbooru.config.other_server_hosts.each do |server|
|
||||
Net::HTTP.delete(URI.parse("http://#{server}/tag_aliases/#{id}/cache"))
|
||||
end
|
||||
end
|
||||
|
||||
def update_cache
|
||||
Cache.put("ta:#{Cache.sanitize(antecedent_name)}", consequent_name)
|
||||
end
|
||||
|
||||
@@ -7,7 +7,8 @@ class Upload < ActiveRecord::Base
|
||||
attr_accessor :file, :image_width, :image_height, :file_ext, :md5, :file_size
|
||||
belongs_to :uploader, :class_name => "User"
|
||||
belongs_to :post
|
||||
before_create :initialize_status
|
||||
before_validation_on_create :initialize_uploader
|
||||
before_validation_on_create :initialize_status
|
||||
before_create :convert_cgi_file
|
||||
validate :uploader_is_not_limited
|
||||
|
||||
@@ -15,7 +16,6 @@ class Upload < ActiveRecord::Base
|
||||
def uploader_is_not_limited
|
||||
if !uploader.can_upload?
|
||||
update_attribute(:status, "error: uploader has reached their daily limit")
|
||||
raise
|
||||
end
|
||||
end
|
||||
|
||||
@@ -28,21 +28,18 @@ class Upload < ActiveRecord::Base
|
||||
def validate_file_exists
|
||||
unless File.exists?(file_path)
|
||||
update_attribute(:status, "error: file does not exist")
|
||||
raise
|
||||
end
|
||||
end
|
||||
|
||||
def validate_file_content_type
|
||||
unless is_valid_content_type?
|
||||
update_attribute(:status, "error: invalid content type (#{file_ext} not allowed)")
|
||||
raise
|
||||
end
|
||||
end
|
||||
|
||||
def validate_md5_confirmation
|
||||
if !md5_confirmation.blank? && md5_confirmation != md5
|
||||
update_attribute(:status, "error: md5 mismatch")
|
||||
raise
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -71,6 +68,8 @@ class Upload < ActiveRecord::Base
|
||||
update_attribute(:status, "error: " + post.errors.full_messages.join(", "))
|
||||
end
|
||||
end
|
||||
rescue Exception => x
|
||||
update_attribute(:status, "error: #{x} - #{x.message}")
|
||||
end
|
||||
|
||||
def convert_to_post
|
||||
@@ -224,7 +223,7 @@ class Upload < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def temp_file_path
|
||||
File.join(Dir::tmpdir, "#{Time.now.to_f}.#{$PROCESS_ID}")
|
||||
File.join(Rails.root, "tmp", "#{Time.now.to_f}.#{$PROCESS_ID}")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -249,11 +248,11 @@ class Upload < ActiveRecord::Base
|
||||
def convert_cgi_file
|
||||
return if file.blank? || file.size == 0
|
||||
|
||||
self.file_path = temp_file_path
|
||||
|
||||
if file.local_path
|
||||
self.file_path = file.local_path
|
||||
FileUtils.cp(file.local_path, file_path)
|
||||
else
|
||||
self.file_path = temp_file_path
|
||||
|
||||
File.open(file_path, 'wb') do |out|
|
||||
out.write(file.read)
|
||||
end
|
||||
@@ -277,6 +276,13 @@ class Upload < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
module UploaderMethods
|
||||
def initialize_uploader
|
||||
self.uploader_id = CurrentUser.user.id
|
||||
self.uploader_ip_addr = CurrentUser.ip_addr
|
||||
end
|
||||
end
|
||||
|
||||
include ConversionMethods
|
||||
include ValidationMethods
|
||||
include FileMethods
|
||||
@@ -287,6 +293,7 @@ class Upload < ActiveRecord::Base
|
||||
include FilePathMethods
|
||||
include CgiFileMethods
|
||||
include StatusMethods
|
||||
include UploaderMethods
|
||||
|
||||
def presenter
|
||||
@presenter ||= UploadPresenter.new(self)
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
class WikiPage < ActiveRecord::Base
|
||||
attr_accessor :updater_id, :updater_ip_addr
|
||||
before_save :normalize_title
|
||||
before_create :initialize_creator
|
||||
after_save :create_version
|
||||
belongs_to :creator, :class_name => "User"
|
||||
belongs_to :updater, :class_name => "User"
|
||||
validates_uniqueness_of :title, :case_sensitive => false
|
||||
validates_presence_of :body, :updater_id, :updater_ip_addr
|
||||
attr_accessible :title, :body, :updater_id, :updater_ip_addr
|
||||
validates_presence_of :body
|
||||
attr_accessible :title, :body
|
||||
scope :titled, lambda {|title| where(["title = ?", title.downcase.tr(" ", "_")])}
|
||||
has_one :tag, :foreign_key => "name", :primary_key => "title"
|
||||
has_one :artist, :foreign_key => "name", :primary_key => "title"
|
||||
@@ -27,12 +25,14 @@ class WikiPage < ActiveRecord::Base
|
||||
relation
|
||||
end
|
||||
|
||||
def self.find_title_and_id(title)
|
||||
titled(title).select("title, id").first
|
||||
end
|
||||
|
||||
def revert_to(version, reverter_id, reverter_ip_addr)
|
||||
self.title = version.title
|
||||
self.body = version.body
|
||||
self.is_locked = version.is_locked
|
||||
self.updater_id = reverter_id
|
||||
self.updater_ip_addr = reverter_ip_addr
|
||||
end
|
||||
|
||||
def revert_to!(version, reverter_id, reverter_ip_addr)
|
||||
@@ -55,8 +55,8 @@ class WikiPage < ActiveRecord::Base
|
||||
def create_version
|
||||
if title_changed? || body_changed? || is_locked_changed?
|
||||
versions.create(
|
||||
:updater_id => updater_id,
|
||||
:updater_ip_addr => updater_ip_addr,
|
||||
:updater_id => CurrentUser.user.id,
|
||||
:updater_ip_addr => CurrentUser.ip_addr,
|
||||
:title => title,
|
||||
:body => body,
|
||||
:is_locked => is_locked
|
||||
@@ -65,8 +65,6 @@ class WikiPage < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def initialize_creator
|
||||
if creator.nil?
|
||||
self.creator_id = updater_id
|
||||
end
|
||||
self.creator_id = CurrentUser.user.id
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user