fix the creation of theme assets (and add tests of course) + upgrade Gemfile + remove the mongoid patches

This commit is contained in:
dinedine 2011-02-04 10:39:23 +01:00
parent 2402ef2cd4
commit c178346969
11 changed files with 122 additions and 176 deletions

12
Gemfile
View File

@ -2,26 +2,24 @@ source :rubygems
# add in all the runtime dependencies
gem 'rails', '>= 3.0.0'
gem 'rails', '>= 3.0.3'
gem 'warden'
gem 'devise', '= 1.1.3'
gem 'mongoid', '~> 2.0.0.rc.6'
gem 'bson_ext', '1.2.0'
# gem 'locomotive_mongoid_acts_as_tree', :path => '../gems/acts_as_tree', :require => 'mongoid_acts_as_tree' # '0.1.5.1', :require => 'mongoid_acts_as_tree'
gem 'locomotive_mongoid_acts_as_tree', '0.1.5.2', :require => 'mongoid_acts_as_tree'
gem 'will_paginate'
gem 'haml', '= 3.0.18'
gem 'haml', '3.0.25'
gem 'locomotive_liquid', '2.2.2', :require => 'liquid'
gem 'formtastic', '~> 1.1.0'
gem 'inherited_resources', '1.1.2'
gem 'formtastic', '~> 1.2.3'
gem 'inherited_resources', '~> 1.1.2'
gem 'rmagick', '= 2.12.2'
gem 'rmagick', '2.12.2'
gem 'locomotive_carrierwave', '0.5.0.1.beta2', :require => 'carrierwave'
# gem 'custom_fields', :path => '../gems/custom_fields' #'1.0.0.beta2'
gem 'custom_fields', '1.0.0.beta.4'
gem 'fog', '0.3.7'
gem 'mimetype-fu'

View File

@ -1,8 +1,8 @@
GIT
remote: git://github.com/floehopper/mocha.git
revision: cc436649221e1895b5fc392247ce108db5ba8244
revision: aae7fdfdff52bb759cca74a2459ce105bba6ff10
specs:
mocha (0.9.10.20101125155727)
mocha (0.9.11.20110203233621)
rake
GEM
@ -106,14 +106,14 @@ GEM
nokogiri (~> 1.4.3.1)
ruby-hmac
formatador (0.0.16)
formtastic (1.1.0)
actionpack (>= 2.3.0)
activesupport (>= 2.3.0)
i18n (>= 0.4.0)
formtastic (1.2.3)
actionpack (>= 2.3.7)
activesupport (>= 2.3.7)
i18n (~> 0.4)
gherkin (2.1.5)
trollop (~> 1.16.2)
growl-glue (1.0.7)
haml (3.0.18)
haml (3.0.25)
has_scope (0.5.0)
heroku (1.16.2)
json_pure (>= 1.2.0, < 1.5.0)
@ -130,7 +130,7 @@ GEM
yui-compressor (>= 0.9.1)
json (1.5.1)
json_pure (1.4.6)
kgio (2.1.1)
kgio (2.2.0)
launchy (0.3.7)
configuration (>= 0.0.5)
rake (>= 0.8.1)
@ -268,12 +268,12 @@ DEPENDENCIES
devise (= 1.1.3)
factory_girl_rails
fog (= 0.3.7)
formtastic (~> 1.1.0)
formtastic (~> 1.2.3)
growl-glue
haml (= 3.0.18)
haml (= 3.0.25)
heroku
httparty (>= 0.6.1)
inherited_resources (= 1.1.2)
inherited_resources (~> 1.1.2)
launchy
locomotive_carrierwave (= 0.5.0.1.beta2)
locomotive_jammit-s3
@ -283,7 +283,7 @@ DEPENDENCIES
mocha!
mongoid (~> 2.0.0.rc.6)
pickle
rails (>= 3.0.0)
rails (>= 3.0.3)
rmagick (= 2.12.2)
rspec-rails (= 2.3.1)
ruby-debug

View File

@ -10,9 +10,10 @@ module Admin
respond_to :json, :only => [:create, :update]
before_filter :sanitize_params, :only => [:create, :update]
def index
@assets = current_site.theme_assets.visible(params[:all]).order_by([[:slug, :asc]])
@assets = @assets.group_by { |a| a.folder.split('/').first.to_sym }
@assets = ThemeAsset.all_grouped_by_folder(current_site, params[:all])
@js_and_css_assets = (@assets[:javascripts] || []) + (@assets[:stylesheets] || [])
if request.xhr?
@ -23,14 +24,7 @@ module Admin
end
end
def edit
resource.performing_plain_text = true if resource.stylesheet_or_javascript?
edit!
end
def create
params[:theme_asset] = { :source => params[:file] } if params[:file]
create! do |success, failure|
success.json do
render :json => {
@ -45,6 +39,15 @@ module Admin
end
end
protected
def sanitize_params
params[:theme_asset] = { :source => params[:file] } if params[:file]
performing_plain_text = params[:theme_asset][:performing_plain_text]
params[:theme_asset].delete(:content_type) if performing_plain_text.blank? || performing_plain_text == 'false'
end
end
end

View File

@ -20,9 +20,10 @@ class ThemeAsset
index [[:site_id, Mongo::ASCENDING], [:local_path, Mongo::ASCENDING]]
## callbacks ##
after_initialize :set_performing_plain_text_boolean
before_validation :store_plain_text
before_save :sanitize_folder
before_save :build_local_path
before_validation :sanitize_folder
before_validation :build_local_path
## validations ##
validates_presence_of :site, :source
@ -98,8 +99,18 @@ class ThemeAsset
{ :url => self.source.url }.merge(self.attributes)
end
def self.all_grouped_by_folder(site, include_all = true)
assets = site.theme_assets.visible(include_all).order_by([[:slug, :asc]])
assets.group_by { |a| a.folder.split('/').first.to_sym }
end
protected
def set_performing_plain_text_boolean
self.performing_plain_text = true if self.stylesheet_or_javascript?
end
def safe_source_filename
self.source_filename || self.source.send(:original_filename) rescue nil
end
@ -117,7 +128,11 @@ class ThemeAsset
end
def build_local_path
self.local_path = File.join(self.folder, self.safe_source_filename)
if filename = self.safe_source_filename
self.local_path = File.join(self.folder, filename)
else
nil
end
end
def escape_shortcut_urls(text)

View File

@ -19,6 +19,7 @@ fr:
presentation: Présentation
attributes: Propriétés
upload: Envoi au serveur
labels:
theme_asset:
plain_text_name: Nom du fichier
@ -52,7 +53,7 @@ fr:
meta_description: "Description utilisée à l'intérieur de la balise HEAD. Requis pour un meilleur référencement."
domain_name: "ex: locomotiveapp.org"
theme_asset:
slug: "Vous n'avez pas besoin de mettre l'extension du fichier (.css ou .js)"
plain_text_name: "Vous n'avez pas besoin de mettre l'extension du fichier (.css ou .js)"
edit:
source: "Vous pouvez le remplacer par un fichier avec la meme extension."
custom_fields:

View File

@ -1,9 +1,9 @@
BOARD:
- moving to mongoid 2.0.0 rc.6
- accepts_nested_attributes (javascript part)
- check the theme uploader
- release new version of CustomFields, ActsAsTree gems
x moving to mongoid 2.0.0 rc.6
x accepts_nested_attributes (javascript part)
x check the theme uploader
x release new version of CustomFields, ActsAsTree gems
BACKLOG:

View File

@ -0,0 +1,51 @@
Feature: Manage Theme Assets
In order to manage theme assets
As an administrator
I want to add/edit/delete theme assets of my site
Background:
Given I have the site: "test site" set up
And I am an authenticated user
Scenario: Theme assets list is not accessible for non authenticated accounts
Given I am not authenticated
When I go to theme assets
Then I should see "Log in"
Scenario: Uploading a valid image
When I go to theme assets
And I follow "new file"
And I attach the file "spec/fixtures/assets/5k.png" to "File"
And I press "Create"
Then I should see "File was successfully created."
And I should not see "Code"
And I should see "images/5k.png"
Scenario: Uploading a stylesheet
When I go to theme assets
And I follow "new file"
And I attach the file "spec/fixtures/assets/main.css" to "File"
And I press "Create"
Then I should see "File was successfully created."
And I should see "Code"
And I should see "stylesheets/main.css"
Scenario: Uploading a javascript
When I go to theme assets
And I follow "new file"
And I fill in "Folder" with "javascripts/test"
And I attach the file "spec/fixtures/assets/application.js" to "File"
And I press "Create"
Then I should see "File was successfully created."
And I should see "Code"
And I should see "javascripts/test/application.js"
Scenario: Uploading an image which already exists
When I go to theme assets
And I follow "new file"
And I attach the file "spec/fixtures/assets/5k.png" to "File"
And I press "Create"
And I follow "new file"
And I attach the file "spec/fixtures/assets/5k.png" to "File"
And I press "Create"
Then I should see "File was not created."

View File

@ -43,4 +43,6 @@ Locomotive.configure do |config|
end
Capybara.default_host = 'test.example.com'
# Capybara.app_host = 'http://test.example.com'
# Capybara.app_host = 'http://test.example.com'
require File.expand_path(File.dirname(__FILE__) + '/../../spec/support/carrierwave')

View File

@ -16,6 +16,8 @@ module NavigationHelpers
destroy_admin_session_path
when /pages/
admin_pages_path
when /theme assets/
admin_theme_assets_path
# Add more mappings here.
# Here is an example that pulls values out of the Regexp:

View File

@ -1,139 +1,3 @@
require 'mongoid'
# require 'mongoid/document'
module Mongoid #:nodoc:
module Validations #:nodoc:
class AssociatedValidator < ActiveModel::EachValidator
def validate_each(document, attribute, value)
values = value.is_a?(Array) ? value : [ value ]
return if values.collect { |doc| doc.nil? || doc.valid? }.all?
document.errors.add(attribute, :invalid, options.merge(:value => value)) # was causing "can't modify frozen hash"
end
end
end
end
# Mongoid causes the following warning "Collection#group no longer take a list of paramters. This usage is deprecated.Check out the new API at http://api.mongodb.org/ruby/current/Mongo/Collection.html#group-instance_method"
# it's already corrected in the mongoid master branch so waiting for the new gem to remove this patch
module Mongoid #:nodoc:
module Contexts #:nodoc:
class Mongo
def aggregate
klass.collection.group(
:key => options[:fields],
:cond => selector,
:initial => { :count => 0 },
:reduce => Javascript.aggregate
)
end
def group
klass.collection.group(
:key => options[:fields],
:cond => selector,
:initial => { :group => [] },
:reduce => Javascript.group
).collect do |docs|
docs["group"] = docs["group"].collect do |attrs|
Mongoid::Factory.build(klass, attrs)
end
docs
end
end
def grouped(start, field, reduce)
collection = klass.collection.group(
:cond => selector,
:initial => { start => "start" },
:reduce => reduce.gsub("[field]", field)
)
collection.empty? ? nil : collection.first[start.to_s]
end
end
end
end
# # http://github.com/emk/mongoid/blob/503e346b1b7b250d682a12332ad9d5872f1575e6/lib/mongoid/atomicity.rb
# module Mongoid #:nodoc:
# module Atomicity #:nodoc:
# extend ActiveSupport::Concern
#
# def _updates
# processed = {}
#
# _children.inject({ "$set" => _sets, "$pushAll" => {}, :other => {} }) do |updates, child|
# changes = child._sets
# updates["$set"].update(changes)
# unless changes.empty?
# processed[child._conficting_modification_key] = true
# end
#
# # MongoDB does not allow "conflicting modifications" to be
# # performed in a single operation. Conflicting modifications are
# # detected by the 'haveConflictingMod' function in MongoDB.
# # Examination of the code suggests that two modifications (a $set
# # and a $pushAll, for example) conflict if (1) the key paths being
# # modified are equal or (2) one key path is a prefix of the other.
# # So a $set of 'addresses.0.street' will conflict with a $pushAll
# # to 'addresses', and we will need to split our update into two
# # pieces. We do not, however, attempt to match MongoDB's logic
# # exactly. Instead, we assume that two updates conflict if the
# # first component of the two key paths matches.
# if processed.has_key?(child._conficting_modification_key)
# target = :other
# else
# target = "$pushAll"
# end
#
# child._pushes.each do |attr, val|
# if updates[target].has_key?(attr)
# updates[target][attr] << val
# else
# updates[target].update({attr => [val]})
# end
# end
# updates
# end.delete_if do |key, value|
# value.empty?
# end
# end
#
# protected
# # Get the key used to check for conflicting modifications. For now, we
# # just use the first component of _path, and discard the first period
# # and everything that follows.
# def _conficting_modification_key
# _path.sub(/\..*/, '')
# end
# end
# end
# ## various patches
# module Mongoid #:nodoc:
# module Document
#
# def update_child_with_noname(child, clear = false)
# name = child.association_name
# return if name.blank? # fix a weird bug with mongoid-acts-as-tree
# update_child_without_noname(child, clear)
# end
#
# alias_method_chain :update_child, :noname
#
# # module ClassMethods
# #
# # def instantiate(attrs = nil, allocating = false) # used by carrierwave to back up the original file
# # document = super
# # document.send(:run_callbacks, :initialize) do
# # document
# # end
# # end
# #
# # end
#
# end
# end
## For now, no mongoid monkey patch is required, that is a good thing after all :-)

View File

@ -51,14 +51,24 @@ describe ThemeAsset do
end
describe 'file is not allowed' do
describe '#validation' do
it 'should not be valid' do
it 'does not accept text file' do
@asset.source = FixturedAsset.open('wrong.txt')
@asset.valid?.should be_false
@asset.errors[:source].should_not be_blank
end
it 'is not valid if another file with the same path exists' do
@asset.source = FixturedAsset.open('5k.png')
@asset.save!
another_asset = Factory.build(:theme_asset, :site => @asset.site)
another_asset.source = FixturedAsset.open('5k.png')
another_asset.valid?.should be_false
another_asset.errors[:local_path].should_not be_blank
end
end
it 'should process stylesheet' do