Permalink
Browse files

Javascript model now loads properly from the filesystem.

  • Loading branch information...
nelstrom committed Nov 16, 2008
1 parent 321fa79 commit d52e22c90b1da3d02ec3c76303f03c163d17aedf
Showing with 27 additions and 19 deletions.
  1. +27 −19 lib/file_system/model/javascript_extensions.rb
@@ -10,34 +10,42 @@ module ClassMethods
def klass_name
"Javascript"
end
+
+ def load_files
+ files = Dir[path + "/**"]
+ unless files.blank?
+ records_on_filesystem = []
+ files.each do |file|
+ record = find_or_initialize_by_filename(File.basename(file))
+ puts "Loading #{self.name.downcase} from #{File.basename(file)}"
+ record.load_file(file)
+ record.save
+ records_on_filesystem << record
+ end
+ fileless_db_records = records_on_database - records_on_filesystem
+ fileless_db_records.each { |item| delete_record(item) }
+ end
+ end
+
+
+ def extract_name(basename)
+ name_parts = basename.split(".")
+ type_or_filter = name_parts.pop
+ name_parts.pop if name_parts[-1] == "min"
+ name = name_parts.join(".")
+ end
end
def load_file(file)
- # filename_regex = /^(?:(\d+)_)?([^.]+)(?:\.([\-\w]+))?/
- # name, type_or_filter = $2, $3 if File.basename(file) =~ filename_regex
- #
- # basename = File.basename(file)
- # name_parts = basename.count(".")
- # filename_regex = /([^.]+)#{"(?:\.([^.]+))?" * name_parts}/
- # if basename =~ filename_regex
- # captures = Regexp.last_match.captures
- # type_or_filter = captures.pop
- # if captures[-1] == "min"
- # min = true
- # captures.pop
- # end
- # name = captures.join(".")
- # end
-
name, mini, type_or_filter = extract_attrs_from_filename(File.basename(file))
content = open(file).read
self.name = name
self.content = content
- if respond_to?(:filter_id)
+ if respond_to?(:filter_id)
self.filter_id = filters.include?(type_or_filter) ? type_or_filter.camelize : nil
end
- if respond_to?(:minifiy)
- self.minifiy = mini
+ if respond_to?(:minify)
+ self.minify = mini
end
end

0 comments on commit d52e22c

Please sign in to comment.