Skip to content
Browse files

working sub-topic creation

  • Loading branch information...
1 parent 3176449 commit d35349d4167b4e4b8c2ab5e10dac94848fa3923b Matt Hodgson committed
View
5 app/controllers/topics_controller.rb
@@ -12,6 +12,11 @@ def index
def new
@topic = Topic.new
@topic.property_types.build
+ if params[:tag_id]
+ @tag = Topic.find(params[:tag_id])
+ @topic.tags << @tag
+ @topic.setup_properties
+ end
end
def create
View
24 app/helpers/application_helper.rb
@@ -12,21 +12,29 @@ def body_id
end
def property_display(property)
- display = case property.property_type.type
+ display = case property.property_type.type_name
when 'String' : parameter.value
when 'Date' : Date.parse(parameter.value).to_s(:main_date)
else 'NOT DONE YET'
end
end
+ def property_edit(builder)
+ display = case builder.object.property_type.type_name
+ when 'String' : builder.text_field :value
+ when 'Date' : builder.date_select :value
+ else 'NOT DONE YET'
+ end
+ end
+
def add_property_type_link(name, form)
- link_to_function name do |page|
- property_type = partial 'topics/property_type_form', :builder => form, :property_type => PropertyType.new
- page << %{
- var new_property_type_id = "new_" + new Date().getTime();
- $('property_types').insert({ bottom: "#{ escape_javascript property_type }".replace(/new_\\d+/g, new_property_type_id) });
- }
- end
+ link_to_function name do |page|
+ property_type = partial 'topics/property_type_form', :builder => form, :property_type => PropertyType.new
+ page << %{
+ var new_property_type_id = "new_" + new Date().getTime();
+ $('property_types').insert({ bottom: "#{ escape_javascript property_type }".replace(/new_\\d+/g, new_property_type_id) });
+ }
end
+ end
end
View
1 app/models/property.rb
@@ -1,5 +1,6 @@
class Property < ActiveRecord::Base
belongs_to :topic
+ belongs_to :property_type
end
View
8 app/models/property_type.rb
@@ -2,8 +2,10 @@ class PropertyType < ActiveRecord::Base
VALID_TYPES = %w( String Date Location )
- validates_inclusion_of :type_name, :in => VALID_TYPES
+ has_many :topics_property_types
+ has_many :topics, :through => :topics_property_types
+ has_many :properties
- belongs_to :topic
+ validates_inclusion_of :type_name, :in => VALID_TYPES
-end
+end
View
13 app/models/topic.rb
@@ -1,7 +1,8 @@
class Topic < ActiveRecord::Base
belongs_to :user
has_many :properties
- has_and_belongs_to_many :property_types, :class_name => 'PropertyType', :join_table => :topics_property_types
+ has_many :topics_property_types
+ has_many :property_types, :through => :topics_property_types
has_and_belongs_to_many :tags, :class_name => 'Topic', :join_table => :topics_topics, :foreign_key => :child_id, :association_foreign_key => :parent_id
has_and_belongs_to_many :topics, :class_name => 'Topic', :join_table => :topics_topics, :foreign_key => :parent_id, :association_foreign_key => :child_id
has_many :occurrences, :through => :topics_occurrences
@@ -12,4 +13,14 @@ def properties_to_use
tags.map(&:property_types).flatten
end
+ def setup_properties
+ properties_to_use.each do |property_type|
+ properties << Property.new(:property_type_id => property_type.id) unless property_type_ids.include?(property_type.id)
+ end
+ end
+
+ def property_type_ids
+ properties.map(&:property_type_id)
+ end
+
end
View
6 app/models/topics_property_type.rb
@@ -0,0 +1,6 @@
+class TopicsPropertyType < ActiveRecord::Base
+
+ belongs_to :property_type
+ belongs_to :topic
+
+end
View
10 app/views/topics/_form.html.haml
@@ -4,6 +4,16 @@
=f.text_field :name
%br
%br
+ #properties
+ -@topic.properties.each do |property|
+ -f.fields_for :properties, property do |p|
+ -Rails.logger.info(property.inspect)
+ =p.label property.property_type.name
+ %br
+ =property_edit p
+ %br
+ %br
+
#property_types
-f.fields_for :property_types do |pf|
View
4 app/views/topics/show.html.haml
@@ -9,4 +9,6 @@
=partial 'property_type', :collection => @topic.property_types
.links
- = link_to 'Edit', edit_topic_url(@topic)
+ = link_to 'Edit', edit_topic_url(@topic)
+ &nbsp;|&nbsp;
+ = link_to "Create a new #{@topic.name}", new_topic_url(:tag_id => @topic.id)

0 comments on commit d35349d

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