Skip to content
Browse files

Remove Dependency Tracking (no long useful since Radiant's move to Ra…

…ck::Cache)
  • Loading branch information...
1 parent bf1730d commit 61b0788882df80d921e5a044af07925d47cad429 @chrisparrish chrisparrish committed with Chris Parrish Dec 29, 2009
View
4 app/models/javascript.rb
@@ -1 +1,3 @@
-class Javascript < TextAsset; end
+class Javascript < TextAsset
+ include TextAssetStandardTags
+end
View
4 app/models/stylesheet.rb
@@ -1 +1,3 @@
-class Stylesheet < TextAsset; end
+class Stylesheet < TextAsset
+ include TextAssetStandardTags
+end
View
86 app/models/text_asset.rb
@@ -6,7 +6,6 @@ class TextAsset < ActiveRecord::Base
# Associations
belongs_to :created_by, :class_name => 'User'
belongs_to :updated_by, :class_name => 'User'
- has_one :dependency, :class_name => 'TextAssetDependency', :dependent => :destroy
validates_presence_of :name, :message => 'required'
validates_length_of :name, :maximum => 100, :message => '{{count}}-character limit'
@@ -16,68 +15,30 @@ class TextAsset < ActiveRecord::Base
object_id_attr :filter, TextAssetFilter
+
include Radiant::Taggable
class TagError < StandardError; end
- def after_initialize
- self.dependency = TextAssetDependency.new(:names => []) if self.new_record?
- # add radius tags to the inherited class now that it has initialized
- create_tags
- end
-
-
- # Ensures that the associated 'dependency' model is saved and alerts potential
- # dependants that this instance has been updated
- def after_save
- self.dependency.names = parse_dependency_names
- self.dependency.effectively_updated_at = self.updated_at
- self.dependency.save
- update_dependants(self.updated_at)
- end
-
-
- # Alert all potential dependants that this instance has been updated
- def after_destroy
- update_dependants(Time.now)
- end
-
-
# URL relative to the web root (accounting for Sns::Config settings)
def url
"/" + Sns::Config["#{self.class.to_s.underscore}_directory"] +
"/" + self.name
end
- # Convenience method
- def effectively_updated_at
- self.dependency.effectively_updated_at
- end
-
-
- # This method is called from outside to notify this instance that another
- # text asset has been updated.
- def process_newly_updated_dependency(name, time)
- if self.dependency.names.include?(name)
- self.dependency.update_attribute('effectively_updated_at', time)
- end
- end
-
# Parses, and filters the current content for output
def render
self.filter.filter(parse(self.content))
end
# Parses the content using a TextAssetContext
- def parse(text, show_errors = true)
- @parsed_dependency_names = []
+ def parse(text)
unless @parser and @context
@context = TextAssetContext.new(self)
@parser = Radius::Parser.new(@context, :tag_prefix => 'r')
end
- @context.show_errors = show_errors
@parser.parse(text)
end
@@ -103,47 +64,4 @@ def self.create_from_file(file)
@text_asset
end
-
- private
-
- # Parses the content and builds an array of all refrenced text_assets (from
- # within tags).
- def parse_dependency_names
- parse(self.content, false)
- @parsed_dependency_names.uniq
- end
-
-
- # Finds all text_assets of the same class and alerts each that this instance
- # has just been updated
- def update_dependants(time)
- self.class.find(:all).each do |other_text_asset|
- unless other_text_asset.name == self.name
- other_text_asset.process_newly_updated_dependency(self.name, time)
- end
- end
- end
-
-
- # Adds a tag named after the inheriting class name (i.e. <r:javascript> or
- # <r:stylesheet>). This method is kind of funky since we wanted to define
- # the tag in only one place yet we don't have the inheriting class' name
- # until after initialization.
- def create_tags
- self.class.class_eval do
- tag(self.name.underscore) do |tag|
- if name = tag.attr['name']
- @parsed_dependency_names << name.strip
- if text_asset = self.class.find_by_name(name.strip)
- text_asset.render
- else
- raise TagError.new("#{self.class.to_s.underscore} with name '#{name}' not found")
- end
- else
- raise TagError.new("`#{self.class.to_s.underscore}' tag must contain a `name' attribute.") unless tag.attr.has_key?('name')
- end
- end
- end
- end
-
end
View
8 app/models/text_asset_context.rb
@@ -1,7 +1,6 @@
class TextAssetContext < Radius::Context
def initialize(text_asset)
- @show_errors = true
super()
globals.text_asset = text_asset
text_asset.tags.each do |name|
@@ -10,11 +9,6 @@ def initialize(text_asset)
end
- def show_errors=(value)
- @show_errors = value
- end
-
-
def render_tag(name, attributes = {}, &block)
super
rescue Exception => e
@@ -34,7 +28,7 @@ def tag_missing(name, attributes = {}, &block)
private
def raise_errors?
- RAILS_ENV != 'production' && @show_errors
+ RAILS_ENV != 'production'
end
end
View
4 app/models/text_asset_dependency.rb
@@ -1,4 +0,0 @@
-class TextAssetDependency < ActiveRecord::Base
- belongs_to :text_asset
- serialize :names, Array
-end
View
26 app/models/text_asset_standard_tags.rb
@@ -0,0 +1,26 @@
+module TextAssetStandardTags
+
+ class TagError < StandardError; end
+
+ def self.included(base)
+ base.class_eval do
+
+ # declares the <r:stylesheet /> OR <r:javascript /> tag depending on the
+ # class type of base.
+ tag_name = base.name.to_s.underscore
+ tag tag_name do |tag|
+ if name = tag.attr['name']
+ if named_asset = base.find_by_name(name.strip)
+ named_asset.render
+ else
+ raise TagError.new("#{tag_name} with name `#{name}' not found")
+ end
+ else
+ raise TagError.new("`#{tag_name}' tag must contain a `name' attribute.")
+ end
+ end
+
+ end
+ end
+
+end
View
4 db/migrate/001_create_text_assets.rb
@@ -1,5 +1,4 @@
class CreateTextAssets < ActiveRecord::Migration
-
def self.up
create_table :text_assets do |t|
t.string :class_name, :limit => 25
@@ -24,5 +23,4 @@ def self.down
say "Removed config for #{config_name}"
end
end
-
-end
+end
View
18 db/migrate/002_add_dependencies_to_text_assets.rb
@@ -1,21 +1,5 @@
class AddDependenciesToTextAssets < ActiveRecord::Migration
def self.up
- if TextAsset.count > 0
- puts "\n * * * * NOTICE * * * *"
- puts " This migration used to populate the newly-created dependencies"
- puts " table but the previous models have since changed. So, now"
- puts " this step only creates the empty table."
- puts "\n Normally, this ist a problem..."
- puts "\n BUT we have detected existing records in your text_assets table"
- puts " which now won't have associated text_asset_dependency records."
- puts "\n To solve this, you will need to run the following rake task"
- puts " once you've fully migrated your database:"
- puts " rake #{RAILS_ENV} radiant:extensions:sns:rebuild_dependencies"
- puts "\n * * * * * * * * * * * *\n\n"
- puts "Press 'enter' to acknowledge and proceed with the migration(s):"
- STDIN.gets
- end
-
create_table :text_asset_dependencies do |t|
t.integer :text_asset_id
t.string :list
@@ -27,4 +11,4 @@ def self.up
def self.down
drop_table :text_asset_dependencies
end
-end
+end
View
13 db/migrate/005_remove_text_asset_dependencies_table.rb
@@ -0,0 +1,13 @@
+class RemoveTextAssetDependenciesTable < ActiveRecord::Migration
+ def self.up
+ drop_table :text_asset_dependencies
+ end
+
+ def self.down
+ create_table :text_asset_dependencies do |t|
+ t.integer :text_asset_id
+ t.string :names
+ t.datetime :effectively_updated_at
+ end
+ end
+end
View
19 lib/tasks/sns_extension_tasks.rake
@@ -14,7 +14,24 @@ namespace :radiant do
task :migrate => :environment do
require 'radiant/extension_migrator'
if ENV["VERSION"]
- SnsExtension.migrator.migrate(ENV["VERSION"].to_i)
+ if [2, 3, 4].include? ENV["VERSION"].to_i
+ puts "\n * * * * NOTICE * * * *"
+ puts "\n Some of the older functionality of SnS has been removed and as a result"
+ puts " several of the middle migrations no longer produce their previous results."
+ puts "\n This isn't an issue when you're just passing through, but you"
+ puts " can no longer safely stop at migration levels 002-004 anymore."
+ puts "\n Your migration will not be run."
+ puts "\n P.S. If you really must revert to one of these older migrations"
+ puts " (levels 002-004), you'll first need to down-migrate to level 001,"
+ puts " then install the older version of SnS and use that to migrate up."
+ puts "\n See db\\migrate\\README for more info."
+ puts "\n * * * * * * * * * * * *\n\n"
+ puts "Press any key to acknowledge and cancel this migration(s):"
+ STDIN.gets
+ raise "Invalid Migration Level Error"
+ else
+ SnsExtension.migrator.migrate(ENV["VERSION"].to_i)
+ end
else
SnsExtension.migrator.migrate
end
View
55 spec/controllers/site_controller_ext_spec.rb
@@ -179,61 +179,6 @@
end
-
-
- describe "with regard to Last-Modified date" do
-
- before :each do
- @dependant = current_asset[:class].new(:name => 'dependant')
- @dependency = current_asset[:class].new(:name => 'dependency')
- save_asset_at(@dependant, 1990)
- end
-
-
- it "should be a string" do
- get :show_page,
- :url => current_asset[:default_directory].split("/") << 'dependant'
- response.headers['Last-Modified'].should be_kind_of(String)
- end
-
-
- it "should use a valid HTTP header date format" do
- get :show_page,
- :url => current_asset[:default_directory].split("/") << 'dependant'
- response.headers['Last-Modified'].should == "Mon, 01 Jan 1990 00:00:00 GMT"
- end
-
-
- it "should reflect the #{current_asset[:name]}'s updated_at date/time if the file has no dependencies" do
- get :show_page,
- :url => current_asset[:default_directory].split("/") << 'dependant'
- response.headers['Last-Modified'].should == Time.utc(1990).httpdate
- end
-
-
- it "should reflect the #{current_asset[:name]}'s updated_at date/time if its dependencies are older" do
- @dependant.content = %{<r:#{current_asset[:name]} name="dependency" />}
- save_asset_at(@dependency, 1991)
- save_asset_at(@dependant, 1992)
-
- get :show_page,
- :url => current_asset[:default_directory].split("/") << 'dependant'
- response.headers['Last-Modified'].should == Time.utc(1992).httpdate
- end
-
-
- it "should reflect the #{current_asset[:name]}'s dependency's updated_at date/time if its dependencies are newer" do
- @dependant.content = %{<r:#{current_asset[:name]} name="dependency" />}
- save_asset_at(@dependant, 1993)
- save_asset_at(@dependency, 1994)
-
- get :show_page,
- :url => current_asset[:default_directory].split("/") << 'dependant'
- response.headers['Last-Modified'].should == Time.utc(1994).httpdate
- end
-
- end
-
end
end
View
245 spec/models/stylesheet_and_javascript_dependency_spec.rb
@@ -1,245 +0,0 @@
-# This specifies the dependency behaviors of Javascripts and Stylesheets (added
-# in version 0.3). These behaviors allow the text assets to track dependencies
-# created by <r:stylesheet> or <r:javascript> tags when used in a text asset
-# context and maintain an effectively_updated_at date to ensure proper caching
-# of stylesheets and javascripts.
-
-require File.dirname(__FILE__) + '/../spec_helper'
-
-[ { :class => Stylesheet,
- :name => 'stylesheet',
- :default_mime_type => 'text/css',
- :inline_element => 'style' },
-
- { :class => Javascript,
- :name => 'javascript',
- :default_mime_type => 'text/javascript',
- :inline_element => 'script' }
-
-].each do |current_tag|
-
- describe "During save, #{current_tag[:name].pluralize} containing <r:#{current_tag[:name]}> tags" do
-
- before :each do
- @text_asset = current_tag[:class].new(:name => 'dependant')
- @text_asset.content = %{<r:#{current_tag[:name]} name="main" />}
- end
-
-
- it 'should log one dependency if only one tag' do
- @text_asset.save!
- @text_asset.dependency.names.should == ["main"]
- end
-
-
- it 'should log a dependency even if the reference file does not exist' do
- @text_asset.content = %{<r:#{current_tag[:name]} name="a_nonexistent_file" />}
- @text_asset.save!
- @text_asset.dependency.names.should == ["a_nonexistent_file"]
- end
-
-
- it 'should log only one dependency if mulitiple tags all reference the the same file' do
- @text_asset.content << %{<r:#{current_tag[:name]} name="main" />}
- @text_asset.save!
- @text_asset.dependency.names.should == ["main"]
- end
-
-
- it 'should log multiple dependencies if multiple files are referenced' do
- @text_asset.content << %{<r:#{current_tag[:name]} name="another_file" />}
- @text_asset.save!
- @text_asset.dependency.names.sort.should == ["another_file", "main"]
- end
-
-
- it 'should log only each dependency only once' do
- @text_asset.content << %{<r:#{current_tag[:name]} name="another_file" />}
- @text_asset.content << %{<r:#{current_tag[:name]} name="main" />}
- @text_asset.save!
- @text_asset.dependency.names.sort.should == ["another_file", "main"]
- end
-
- end
-
-
-
-
- describe "A saved #{current_tag[:name]}'s effectively_updated_at method" do
- before :each do
- @dependant = current_tag[:class].new(:name => 'dependant')
- save_asset_at(@dependant, 1980)
- end
-
-
- it "should reflect its own creation date/time (created_at) if the file hasn't been updated and has no dependencies" do
- @dependant.effectively_updated_at.should == Time.utc(1980)
- end
-
-
- it "should reflect its own change date/time (updated_at) if the file has no dependencies" do
- save_asset_at(@dependant, 1981)
- @dependant.effectively_updated_at.should == Time.utc(1981)
- end
-
-
- it "should reflect its own change date/time if it references dependencies which do not exist" do
- @dependant.content = %{<r:#{current_tag[:name]}name="a_bogus_text_asset" />}
- save_asset_at(@dependant, 1982)
- @dependant.effectively_updated_at.should == Time.utc(1982)
- end
-
-
- it "should reflect a dependency's creation date/time when that dependency (which previously didn't exist) is created" do
- @dependant.content = %{<r:#{current_tag[:name]} name="dependency" />}
- save_asset_at(@dependant, 1983)
-
- @dependency = current_tag[:class].new(:name => 'dependency')
- save_asset_at(@dependency, 1984)
-
- @dependant = current_tag[:class].find_by_name('dependant')
- @dependant.effectively_updated_at.should == Time.utc(1984)
- end
-
-
- it "should reflect its own change date/time when it is updated" do
- @dependant.content = %{<r:#{current_tag[:name]} name="dependency" />}
- save_asset_at(@dependant, 1985)
-
- @dependency = current_tag[:class].new(:name => 'dependency')
- save_asset_at(@dependency, 1986)
-
- save_asset_at(@dependant, 1987)
-
- @dependant = current_tag[:class].find_by_name('dependant')
- @dependant.effectively_updated_at.should == Time.utc(1987)
- end
-
-
- it "should reflect a dependency's change date/time once that dependency is updated" do
- @dependency = current_tag[:class].new(:name => 'dependency')
- save_asset_at(@dependency, 1988)
-
- @dependant.content = %{<r:#{current_tag[:name]} name="dependency" />}
- save_asset_at(@dependant, 1989)
-
- save_asset_at(@dependency, 1990)
-
- @dependant = current_tag[:class].find_by_name('dependant')
- @dependant.effectively_updated_at.should == Time.utc(1990)
- end
-
-
- it "should reflect a dependency's deletion date/time when that dependency file is removed" do
- @dependency = current_tag[:class].new(:name => 'dependency')
- save_asset_at(@dependency, 1991)
-
- @dependant.content = %{<r:#{current_tag[:name]} name="dependency" />}
- save_asset_at(@dependant, 1992)
-
- Time.stub!(:now).and_return(Time.utc(1993))
- @dependency.destroy
-
- @dependant = current_tag[:class].find_by_name('dependant')
- @dependant.effectively_updated_at.should == Time.utc(1993)
- end
-
- end
-
-
-
-# These specs deal with TextAsset objects automatically recreating the associated
-# dependency objects (and their values) upon initialization. I'm not sure this
-# is really a requirement but I'll keep the specs here (commented out) since I
-# did the work of creating them in case I change my mind later.
-# describe "an existing #{current_tag[:name]}" do
-# require 'scenarios'
-#
-# before :each do
-# create_record :text_asset, :dependant,
-# :name => 'data-missing',
-# :class_name => current_tag[:class].to_s,
-# :content => "<r:#{current_tag[:name]} name='dependency1'/>" +
-# "<r:#{current_tag[:name]} name='dependency2'/>",
-# :updated_at => Time.utc(1990)
-# create_record :text_asset, :dependency1,
-# :name => 'dependency1',
-# :class_name => current_tag[:class].to_s,
-# :updated_at => Time.utc(1980)
-# create_record :text_asset, :dependency2,
-# :name => 'dependency2',
-# :class_name => current_tag[:class].to_s,
-# :updated_at => Time.utc(2000)
-# end
-#
-#
-#
-#
-# describe "with no associated TextAssetDependency object" do
-#
-# it 'should create a new one during instantiation' do
-# text_assets(:dependant).dependency.id.should ==
-# TextAssetDependency.find_by_text_asset_id(text_assets(:dependant).id)
-# end
-#
-# end
-#
-#
-#
-#
-# describe 'where the dependency.names is not set (nil)' do
-#
-# before :each do
-# # gets the asset (but this will set dependency.names
-# text_asset = text_assets(:dependant)
-# @text_asset_id = @text_asset.id
-# # delete the value for dependency.names and save to db
-# text_asset.dependency.update_attribute('names', nil)
-# end
-#
-#
-# it 'should reparse dependency names during initialization' do
-# text_assets(:dependant).dependency.names.should ==
-# ['dependency1', 'dependency2']
-# end
-#
-#
-# it 'should store the reparsed dependency names in the db' do
-# text_assets(:dependant)
-# TextAssetDependency.find_by_text_asset_id(@text_asset_id).names.should ==
-# ['dependency1', 'dependency2']
-# end
-#
-# end
-#
-#
-#
-#
-# describe 'where the dependency.effectively_updated_at is not set (nil)' do
-#
-# before :each do
-# # gets the asset (but this will set dependency.names
-# text_asset = text_assets(:dependant)
-# @text_asset_id = @text_asset.id
-# # delete the value for dependency.names and save to db
-# text_asset.dependency.update_attribute('names', nil)
-# end
-#
-#
-# it 'should reparse dependency names during initialization' do
-# text_assets(:dependant).dependency.effectively_updated_at.should ==
-# Time.utc(2000)
-# end
-#
-#
-# it 'should store the reparsed dependency names in the db' do
-# text_assets(:dependant)
-# TextAssetDependency.find_by_text_asset_id(@text_asset_id).effectively_updated_at.should ==
-# Time.utc(2000)
-# end
-#
-# end
-#
-# end
-
-end

0 comments on commit 61b0788

Please sign in to comment.
Something went wrong with that request. Please try again.