I have a little bit of a complex model and want to get the full functionality capable with my limited understanding of rails.
I have a section, a header (which uses acts_as_tree), and an item.
I use json to bring sets of data in. This has worked very well. I would like to be able to bring attributes in for whole sets of data such as 'is_shippable'. I would like to be able to specify anywhere in the tree the is_shippable value and set to true. Also, I would like to be able to override at the header or item level to set it to false.
I have decided that it makes sense to have is_shippable as an attribute on the section, header, and item and try to use a before_create callback to determine whether it should be is_shippable.
For example:
section
header -acts_as_tree
item - is_shippable
sample json:
{
"name":"sample section",
"is_shippable": true,
"headers_attributes":[
{
"name":"sample_section"
"items_attributes":[{
"name":"sample item",
"is_shippable":false,
}
]
}
]
}
in header.rb
before_save :default_values
private
def default_values
self.is_shippable ||=self.section.is_shippable
# need to be able to set header to is_shippable=false if specified explicitly at that level
end
in item.rb
before_save :default_values
private
def default_values
# if not set, default to 0
self.is_shippable ||= 0
self.is_shippable=1 if self.header.is_shippable==true
# need to be able to set item to is_shippable=false if specified explicitly at that level
end
Is there a better way to do this than I am doing? How would I execute in if statements checking if is_shippable is set to false if it has been set to true higher in the hierarchy?
EDIT - also there are more features that is_shippable like is_fragile, is_custom_size etc...