diff --git a/app/inputs/uploader_input.rb b/app/inputs/uploader_input.rb new file mode 100644 index 0000000..fabdbad --- /dev/null +++ b/app/inputs/uploader_input.rb @@ -0,0 +1,162 @@ +# A formtastic input which incorporates carrierwave uploader functionality. +# +# Intelligently adds the cache field, displays and links to the current +# value if there is one, adds a class to the wrapper when replacing an +# existing value, allows removing an existing value with the checkbox +# taking into account validation requirements. +# +# There are several options: +# +# * Toggle the replacement field with `replaceable: true/false`. +# * The replace file label is translatable as `replace_label` or using option `replace_label: "value"` (like `label`). +# * Toggle the remove checkbox with `removable: true/false` (`true` overrides `required`). +# * The remove checkbox label is translatable as `remove_label` or using option `remove_label: "value"` (like `label`). +# * Override existing file display and links using `existing_html` and `existing_link_html` (like `wrapper_html`). +# +# Example: `form.input :file, as: "uploader"` +# +# Copyright (c) Samuel Cochran 2012, under the [MIT license](http://www.opensource.org/licenses/mit-license). +class UploaderInput < Formtastic::Inputs::FileInput + def linkable? + options[:linkable] != false + end + + def replaceable? + options[:replaceable] != false + end + + def removable? + options[:removable] != false and (options[:removable] == true or not required?) + end + + def method_present? + if object.respond_to?("#{method}?") + object.send("#{method}?") + else + object.send(method).present? + end + end + + def method_changed? + if object.respond_to? "#{method}_changed?" + object.send "#{method}_changed?" + else + false + end + end + + def method_was_present? + if not method_changed? + method_present? + else + object.send("#{method}_was").present? + end + end + + def wrapper_html_options + super.tap do |options| + options[:class] << " replaceable" if replaceable? + options[:class] << " removable" if removable? + options[:class] << " present" if method_present? + options[:class] << " changed" if method_changed? + options[:class] << " was_present" if method_was_present? + end + end + + def cache_html + if method_changed? + builder.hidden_field("#{method}_cache") + end or "".html_safe + end + + def file_html + builder.file_field(method, input_html_options) + end + + def existing_html_options + expand_html_options(options[:existing_html]) do |opts| + opts[:class] << "existing" + end + end + + def existing_link_html_options + expand_html_options(options[:existing_link_html]) do |opts| + opts[:class] << "existing" + end + end + + def existing_html + if method_present? + # TODO: Add classes based on mime type for icons, etc. + existing = template.content_tag(:span, template.image_tag( object.send(method).thumb), existing_html_options) + template.link_to_if linkable?, existing, object.send(method).url, existing_link_html_options + end or "".html_safe + end + + def replace_label_html + template.content_tag(:label, class: "replace_label") do + template.content_tag(:span, localized_string(method, "Replace #{method.to_s.titleize}", :replace_label)) + end + end + + def replace_html + if replaceable? + replace_label_html << + file_html + end or "".html_safe + end + + def remove_html + if removable? + template.content_tag(:label, class: "remove_label") do + template.check_box_tag("#{object_name}[remove_#{method}]", "1", false, id: "#{sanitized_object_name}_remove_#{sanitized_method_name}") << + # XXX: There are probably better ways to do these translations using defaults. + template.content_tag(:span, localized_string(method, "Remove #{method.to_s.titleize}", :remove_label)) + end + end or "".html_safe + end + + def to_html + input_wrapping do + label_html << + cache_html << + if method_was_present? + existing_html << + replace_html << + remove_html + else + existing_html << + file_html + end + end + end + +protected + + def expand_html_options opts + (opts || {}).dup.tap do |opts| + opts[:class] = + case opts[:class] + when Array + opts[:class].dup + when nil + [] + else + [opts[:class].to_s] + end + opts[:data] = + case opts[:data] + when Hash + opts[:data].dup + when nil + {} + else + {"" => opts[:data].to_s} + end + + yield opts if block_given? + + opts[:class] = opts[:class].join(' ') + end + end +end diff --git a/app/mailers/news_mailer.rb b/app/mailers/news_mailer.rb index e00a7c9..ca45278 100644 --- a/app/mailers/news_mailer.rb +++ b/app/mailers/news_mailer.rb @@ -2,7 +2,7 @@ class NewsMailer < ActionMailer::Base default from: "salzamt@fet.at" def neuigkeit_mail(email, neuigkeit_id) @neuigkeit= Neuigkeit.find(neuigkeit_id) - + email = "" if Rails.env=="development" mail(to: email, subject: @neuigkeit.title) end def daily_newsletter(user_id) diff --git a/app/models/neuigkeit.rb b/app/models/neuigkeit.rb index 46412f9..e46d204 100755 --- a/app/models/neuigkeit.rb +++ b/app/models/neuigkeit.rb @@ -23,6 +23,7 @@ class Neuigkeit < ActiveRecord::Base has_many :attachments, :as=>:parent has_one :title_pic, :class_name=>"Attachment", :as=>:parent, :conditions=>["attachments.flag_titlepic =?", true] + has_many :questions, :class_name=>"Survey::Question", as: :parent validates :rubrik, :presence=>true validates :author, :presence=>true diff --git a/app/models/survey/choice.rb b/app/models/survey/choice.rb index 0a8aa0d..6477423 100644 --- a/app/models/survey/choice.rb +++ b/app/models/survey/choice.rb @@ -1,6 +1,6 @@ class Survey::Choice < ActiveRecord::Base belongs_to :question, class_name: 'Survey::Question' - attr_accessible :picture, :sort, :text, :icon, :picture_cache + attr_accessible :picture, :sort, :text, :icon, :picture_cache, :remove_picture has_many :answers, class_name: 'Survey::Answer' include ActionView::Helpers::TagHelper mount_uploader :picture, PictureUploader diff --git a/app/views/neuigkeiten/show.html.erb b/app/views/neuigkeiten/show.html.erb index 4c5daf6..cd49339 100755 --- a/app/views/neuigkeiten/show.html.erb +++ b/app/views/neuigkeiten/show.html.erb @@ -36,6 +36,9 @@ <%= render partial: "neuigkeit_view", object: @neuigkeit %> +<% @neuigkeit.questions.each do |q| %> +<%= render q%> +<% end %>