Permalink
Browse files

Make Section a separate entity.

  • Loading branch information...
1 parent aba059d commit 1e07b7a8321fa75841a04e8997b45e7767a3c7fe @threedaymonk threedaymonk committed Apr 24, 2012
Showing with 36 additions and 1 deletion.
  1. +35 −0 app/models/section.rb
  2. +1 −1 app/views/artefacts/_form.html.erb
View
@@ -0,0 +1,35 @@
+class Section
+
+ def initialize(top_level, name)
+ @top_level = top_level
+ @name = name
+ end
+
+ def slug
+ [@top_level, @name].join(":")
+ end
+
+ def to_s
+ [@top_level, @name].join(" > ")
+ end
+
+ class << self
+ def all
+ @sections ||= load_sections
+ end
+
+ private
+ def load_sections
+ top_level = nil
+ File.open(File.expand_path("../sections.txt", __FILE__)).inject([]) { |sections, line|
+ if line =~ /^ /
+ raise "Bad section.txt, must start with a section (no spaces at start of first line)" if top_level.nil?
+ sections << new(top_level, line.strip)
+ else
+ top_level = line.strip
+ sections
+ end
+ }
+ end
+ end
+end
@@ -29,7 +29,7 @@
</span>
<% end %>
</li>
- <%= f.input :section, :as => :select, :collection => Artefact.sections.map {|s| [s.gsub(':', ' > '), s] } %>
+ <%= f.input :section, :as => :select, :collection => Section.all.map { |s| [s.to_s, s.slug] } %>
<% end %>
<%= f.inputs "Related items", :class => "related" do %>
<%= f.semantic_fields_for :related_items, related_items_for(artefact) do |related_item| %>

0 comments on commit 1e07b7a

Please sign in to comment.