Skip to content

add create method HyperModel collection class #134

Description

@catmando

i.e.

something.groups.create(name: 'new group')# roughly the same as new_group << Group.new(name: 'new group')something.groups << new_groupnew_group.save

Note that Hyperstack currently does not save an item when its pushed onto a collection, unless the collection is a has_many :through relationship, hence the final save above.

Like the AR create method this should return a promise / take a block that is resolved when the save completes.

Roughly code:

defcreate(args={})args=[args]unlessargs.is_a?Arrayitems_to_save=if(through_association=@association&.through_association)args.collectdo |attributes|
through_association.klass.new(@association.inverse_of=>@owner,@association.source=>@association.klass.new(attributes))endelseargs.collectdo |attributes|
@association.klass.new(attributes).tap{ |item| _internal_push(item)}endendPromise.new.tapdo |promise|
Promise.when(items_to_save.collect(&:save)).thendopromise.resolve(items_to_save)yield *items_to_saveifblock_given?endendend

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions