new custom field type: File + fix minor issue with Date

This commit is contained in:
dinedine 2010-06-30 15:18:17 +02:00
parent dda2f7ef19
commit a6bde2b8e5
24 changed files with 196 additions and 21 deletions

View File

@ -1,7 +1,7 @@
module Admin::CustomFieldsHelper
def options_for_field_kind(selected = nil)
options = %w{String Text Category Boolean Date}.map do |kind|
options = %w{String Text Category Boolean Date File}.map do |kind|
[t("admin.custom_fields.kind.#{kind.downcase}"), kind]
end
end

View File

@ -17,4 +17,15 @@
= form.custom_input field._alias.to_sym, :label => field.label, :hint => field.hint, :css => 'toggle' do
= form.check_box field._alias.to_sym
- elsif field.date?
= form.input field._alias.to_sym, :label => field.label, :hint => field.hint, :input_html => { :class => 'date' }
= form.input field._alias.to_sym, :label => field.label, :hint => field.hint, :input_html => { :class => 'date' }
- elsif field.file?
= form.custom_input field._alias.to_sym, :label => field.label, :hint => field.hint, :css => 'file' do
= form.file_field field._name.to_sym
- if form.object.send(:"#{field._name}?")
%p
%strong
= link_to File.basename(form.object.send(field._name).url), form.object.send(field._name).url
%span
 / 
= t('.delete_file')
= form.check_box :"remove_#{field._name}"

View File

@ -41,6 +41,7 @@ en:
category: Select
boolean: Checkbox
date: Date
file: File
text_formatting:
none: None
html: HTML
@ -52,6 +53,7 @@ en:
collection_label: List of options
custom_form:
edit_categories: Edit options
delete_file: Delete file
sessions:
new:
@ -208,6 +210,7 @@ en:
choose_plain_text: Choose plain text
images:
title: Listing images
no_items: "There are no files for now."
messages:
successful_create: "File was successfully created."
successful_update: "File was successfully updated."

View File

@ -1,16 +1,11 @@
BOARD:
- asset picker (content instance)
- refactoring admin crud (pages + layouts + snippets)
- refactor slugify method (use parameterize + create a module)
- missing key: en, admin, theme_assets, images, no_items
BACKLOG:
- localize application in French
- localize application in French (tork)
- admin
- devise
- carrierwave
@ -37,6 +32,7 @@ NICE TO HAVE:
- traffic statistics
- Worker => Heroku / S3 (not so sure finally)
- theme asset picker when editing layout
- asset picker (content instance)
- page with regexp url ?
DONE:
@ -48,4 +44,9 @@ x new custom field types
x enable/disable text formatting
x custom fields for asset collections
x [BUG] impossible to remove many assets in an asset collection
x new custom field type: date
x new custom field type: date
x missing key: en, admin, theme_assets, images, no_items
x new custom field type: file
x update custom_fields plugin
x locomotive ui
x liquid templates

View File

@ -5,6 +5,7 @@ require 'locomotive/liquid'
require 'locomotive/mongoid'
require 'locomotive/carrierwave'
require 'locomotive/heroku'
require 'locomotive/custom_fields'
require 'mongo_session_store/mongoid'

View File

@ -1 +1,3 @@
require 'carrierwave/orm/mongoid'
require 'locomotive/carrierwave/base'
require 'locomotive/carrierwave/patches'

View File

@ -0,0 +1,15 @@
module CarrierWave
module Uploader
class Base
def to_liquid
{
:url => self.url,
:filename => File.basename(self.url),
:size => self.size
}.stringify_keys
end
end
end
end

View File

@ -1,5 +1,4 @@
require 'carrierwave'
require 'carrierwave/orm/mongoid'
module CarrierWave

View File

@ -0,0 +1,18 @@
# Set correct paths
module CustomFields
module Types
module File
class FileUploader < ::CarrierWave::Uploader::Base
def store_dir
"sites/#{model.content_type.site_id}/contents/#{model.id}/files"
end
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end
end
end
end

View File

@ -7,10 +7,12 @@ module Locomotive
return '' if @source.nil?
if not @@forbidden_attributes.include?(meth.to_s)
@source.send(meth)
value = @source.send(meth)
end
end
end
end
end

View File

@ -385,6 +385,17 @@ form.content_instance fieldset ol li.text textarea {
width: 75%;
}
form.content_instance fieldset ol li.file p {
margin: 5px 0 0 20%;
}
form.content_instance fieldset ol li.file p a {
text-decoration: none;
color: #333;
}
form.content_instance fieldset ol li.file p a:hover { text-decoration: underline; }
/* ___ my account ___ */
form.formtastic fieldset.language li.full span {

View File

@ -4,6 +4,7 @@ gem 'bson_ext', '>= 1.0.1'
gem 'mongo_ext'
gem 'mongoid', '2.0.0.beta6'
gem 'activesupport', '3.0.0.beta3'
gem 'carrierwave-rails3', :require => 'carrierwave'
group :test do
gem 'rspec', '>= 2.0.0.beta.10'

View File

@ -1,6 +1,7 @@
$:.unshift File.expand_path(File.dirname(__FILE__))
require 'active_support'
require 'carrierwave/orm/mongoid'
require 'custom_fields/extensions/mongoid/document'
require 'custom_fields/extensions/mongoid/associations/proxy'
@ -12,6 +13,7 @@ require 'custom_fields/types/text'
require 'custom_fields/types/category'
require 'custom_fields/types/boolean'
require 'custom_fields/types/date'
require 'custom_fields/types/file'
require 'custom_fields/proxy_class_enabler'
require 'custom_fields/field'
require 'custom_fields/custom_fields_for'

View File

@ -11,6 +11,7 @@ module CustomFields
include Types::Category
include Types::Boolean
include Types::Date
include Types::File
## fields ##
field :label
@ -33,7 +34,7 @@ module CustomFields
def apply(klass)
return unless self.valid?
klass.field self._name, :type => self.field_type
klass.field self._name, :type => self.field_type if self.field_type
apply_method_name = :"apply_#{self.kind.downcase}_type"

View File

@ -14,7 +14,7 @@ module CustomFields
klass.class_eval <<-EOF
def #{self.safe_alias}
self.#{self._name}.strftime(I18n.t('date.formats.default'))
self.#{self._name}.strftime(I18n.t('date.formats.default')) rescue nil
end
def #{self.safe_alias}=(value)

View File

@ -22,7 +22,7 @@ module CustomFields
def register_type(kind, klass = ::String)
self.field_types ||= {}
self.field_types[kind.to_sym] = klass
self.field_types[kind.to_sym] = klass unless klass.nil?
self.class_eval <<-EOF
def #{kind.to_s}?

View File

@ -0,0 +1,27 @@
module CustomFields
module Types
module File
extend ActiveSupport::Concern
included do
register_type :file, nil # do not create the default field
end
module InstanceMethods
def apply_file_type(klass)
klass.mount_uploader self._name, FileUploader
self.apply_default_type(klass)
end
end
class FileUploader < ::CarrierWave::Uploader::Base
end
end
end
end

View File

@ -0,0 +1 @@
Hello world !

View File

@ -0,0 +1,18 @@
require 'spec_helper'
describe CustomFields::Types::File do
before(:each) do
@project = Project.new(:name => 'Locomotive')
@project.task_custom_fields.build(:label => 'Screenshot', :_alias => 'screenshot', :kind => 'File')
@project.save
@task = @project.tasks.build
end
it 'attaches file' do
@task.screenshot = FixturedFile.open('doc.txt')
@task.save
@task.screenshot.url.should == '/uploads/doc.txt'
end
end

View File

@ -1,7 +1,7 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
MODELS = File.join(File.dirname(__FILE__), "models")
MODELS = File.join(File.dirname(__FILE__), 'models')
$LOAD_PATH.unshift(MODELS)
require 'rubygems'
@ -16,12 +16,8 @@ require 'custom_fields'
Dir[ File.join(MODELS, "*.rb") ].sort.each { |file| require File.basename(file) }
Mongoid.configure do |config|
name = "custom_fields_test"
host = "localhost"
config.master = Mongo::Connection.new.db(name)
# config.master = Mongo::Connection.new('localhost', '27017', :logger => Logger.new($stdout)).db(name)
end
require 'support/mongoid'
require 'support/carrierwave'
Rspec.configure do |config|
config.mock_with :mocha

View File

@ -0,0 +1,31 @@
require 'carrierwave/test/matchers'
CarrierWave.configure do |config|
config.storage = :file
config.store_dir = "uploads"
config.cache_dir = "cache"
config.root = File.join(File.dirname(__FILE__), '..', 'tmp')
end
module FixturedFile
def self.open(filename)
File.new(self.path(filename))
end
def self.path(filename)
File.join(File.dirname(__FILE__), '..', 'fixtures', filename)
end
def self.duplicate(filename)
dst = File.join(File.dirname(__FILE__), '..', 'tmp', filename)
FileUtils.cp self.path(filename), dst
dst
end
def self.reset!
FileUtils.rm_rf(File.join(File.dirname(__FILE__), '..', 'tmp'))
FileUtils.mkdir(File.join(File.dirname(__FILE__), '..', 'tmp'))
end
end
FixturedFile.reset!

View File

@ -0,0 +1,6 @@
Mongoid.configure do |config|
name = "custom_fields_test"
host = "localhost"
config.master = Mongo::Connection.new.db(name)
# config.master = Mongo::Connection.new('localhost', '27017', :logger => Logger.new($stdout)).db(name)
end

View File

@ -42,6 +42,12 @@ describe CustomFields::Types::Date do
@project.field_1.should == @date
end
it 'sets nil value' do
@project.started_at = nil
@project.started_at.should be_nil
@project.field_1.should be_nil
end
end
def build_project_with_date

View File

@ -0,0 +1,23 @@
require 'spec_helper'
describe CustomFields::Types::Date do
context 'on field class' do
before(:each) do
@field = CustomFields::Field.new
end
it 'returns true if it is a Date' do
@field.kind = 'File'
@field.file?.should be_true
end
it 'returns false if it is not a Date' do
@field.kind = 'string'
@field.file?.should be_false
end
end
end