hobo_files/plugin/lib/hobo/model.rb in hobo-0.7.4 vs hobo_files/plugin/lib/hobo/model.rb in hobo-0.7.5
- old
+ new
@@ -76,13 +76,11 @@
def named(*args)
raise NoNameError, "Model #{name} has no name attribute" unless name_attribute
send("find_by_#{name_attribute}", *args)
end
- alias_method :[], :named
-
def field_added(name, type, args, options)
self.name_attribute = name.to_sym if options.delete(:name)
self.primary_content_attribute = name.to_sym if options.delete(:primary_content)
self.creator_attribute = name.to_sym if options.delete(:creator)
validate = options.delete(:validate) {true}
@@ -289,21 +287,24 @@
HoboFields.to_name(self) || name.underscore.gsub("/", "__")
end
def manage_join_records(association)
- through = reflections[association].through_reflection
- source = reflections[association].source_reflection
method = "manage_join_records_for_#{association}"
after_save method
class_eval %{
def #{method}
- current = #{through.name}.*.#{source.name}
- to_delete = current - #{association}
- to_add = #{association} - current
- #{through.klass.name}.delete_all(["#{through.primary_key_name} = ? and #{source.primary_key_name} in (?)",
+ assigned = #{association}.dup
+ current = #{association}.reload
+
+ through = #{association}.proxy_reflection.through_reflection
+ source = #{association}.proxy_reflection.source_reflection
+
+ to_delete = current - assigned
+ to_add = assigned - current
+ through.klass.delete_all(["\#{through.primary_key_name} = ? and \#{source.primary_key_name} in (?)",
self.id, to_delete.*.id]) if to_delete.any?
to_add.each { |record| #{association} << record }
end
}
end
@@ -361,11 +362,11 @@
save
end
def user_view(user, field=nil)
- raise PermissionDeniedError unless Hobo.can_view?(user, self, field)
+ raise PermissionDeniedError, self.inspect unless Hobo.can_view?(user, self, field)
end
def user_destroy(user)
raise PermissionDeniedError unless Hobo.can_delete?(user, self)
@@ -509,20 +510,24 @@
end
end
def convert_associated_records_for_mass_assignment(reflection, value)
if reflection.macro.in?([:belongs_to, :has_one])
- if value.is_a?(String) && value.starts_with?('@')
- # TODO: This @foo_1 feature is rarely (never?) used - get rid of it
- Hobo.object_from_dom_id(value[1..-1])
+ if value.is_a?(String)
+ if value.starts_with?('@')
+ # TODO: This @foo_1 feature is rarely (never?) used - get rid of it
+ Hobo.object_from_dom_id(value[1..-1])
+ else
+ reflection.klass.named(value)
+ end
else
value
end
elsif reflection.macro == :has_many
if reflection.klass.try.name_attribute
value.map do |x|
if x.is_a?(String)
- reflection.klass[x] unless x.blank?
+ reflection.klass.named(x) unless x.blank?
else
x
end
end.compact
else