diff --git a/app/controllers/merged/cards_controller.rb b/app/controllers/merged/cards_controller.rb index bffaf91..be50ecc 100644 --- a/app/controllers/merged/cards_controller.rb +++ b/app/controllers/merged/cards_controller.rb @@ -28,7 +28,7 @@ module Merged def new @section = Section.find(params[:section_id]) - new_card = @section.new_card + new_card = @section.new_card redirect_to section_cards_url(@section.id) , notice: "Card created" end diff --git a/app/models/merged/card.rb b/app/models/merged/card.rb index 3562b6e..5b5a643 100644 --- a/app/models/merged/card.rb +++ b/app/models/merged/card.rb @@ -36,7 +36,7 @@ module Merged def save super data = Card.all.collect {|obj| obj.attributes} - File.write( OptionDefinition.full_path , data.to_yaml) + File.write( Card.full_path , data.to_yaml) end def set_index(index) @@ -47,12 +47,14 @@ module Merged CardStyle.find_by_template( section.card_template) end - def self.build_data(card_template) - data = { "id" => SecureRandom.hex(10) } + def self.new_card(card_template , section_id , index) + data = { section_id: section_id , index: index} CardStyle.find_by_template( card_template ).fields.each do |key| - data[key] = key.upcase + data[key] = "NEW" end - data + card = Card.new(data) + card.add_default_options + card end end diff --git a/app/models/merged/optioned.rb b/app/models/merged/optioned.rb index f6f5a80..d3cdf01 100644 --- a/app/models/merged/optioned.rb +++ b/app/models/merged/optioned.rb @@ -27,6 +27,13 @@ module Merged options[option] = value end + def add_default_options + option_definitions.each do |option| + next unless option.default + set_option( option.name , option.default) + end + end + def update(key , value) raise "unsuported field #{key} for #{template}" unless allowed_fields.include?(key) if(! attributes[key].nil? ) # first setting ok, types not (yet?) specified diff --git a/app/models/merged/page.rb b/app/models/merged/page.rb index d795165..268ebba 100644 --- a/app/models/merged/page.rb +++ b/app/models/merged/page.rb @@ -9,8 +9,6 @@ module Merged fields :name , :content , :size , :updated_at - alias :id :name - def sections Section.where(page_id: name).order(index: :asc) end diff --git a/app/models/merged/section.rb b/app/models/merged/section.rb index 0499a19..a8c8a45 100644 --- a/app/models/merged/section.rb +++ b/app/models/merged/section.rb @@ -35,11 +35,8 @@ module Merged end def new_card - card_data = Card.build_data(card_template) - index = cards.length - card = Card.new(self , index, card_data) - @cards << card - @content["cards"] << card_data + card = Card.new_card( card_template , self.id , cards.length) + card.save card end