Created
February 24, 2014 11:04
-
-
Save julesce/9185723 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def async_save | |
| json_v2_response_only do | |
| json = JSON.parse(params[:promotion]) | |
| @promotion = current_user.promotions_find(json["id"]) | |
| validator = PromotionModuleValidator.new(@promotion, json) | |
| if validator.valid? | |
| @promotion.update_from_json(json, true) | |
| # Update the mailing for the RSS Promotion when changing the promotion's name | |
| if @promotion.rss_promotion.present? && @promotion.rss_promotion.mailing.present? | |
| @promotion.rss_promotion.mailing.update_attribute :subject, @promotion.name | |
| end | |
| # Add the promotion to the promotions the user has selected on the dashboard | |
| current_user.add_selected_promotion @promotion.id if current_user.selected_promotions.size > 0 | |
| PromotionPresenter.new(@promotion).to_hash(:composer) | |
| else | |
| raise StandardError, h(validator.errors.to_sentence) | |
| end | |
| end | |
| end | |
| class PromotionModuleValidator | |
| attr_reader :promotion, :json | |
| def initialize(promotion, json) | |
| @promotion = promotion | |
| @json = json | |
| end | |
| def valid? | |
| errors.blank? | |
| end | |
| def errors | |
| @errors ||= determine_errors | |
| end | |
| private | |
| def determine_errors | |
| problems = [] | |
| modules = promotion.promotion_modules.all | |
| json["modules"].each_with_index do |module_json, position| | |
| promotion_module = modules.detect { |m| m.id == module_json["id"] } | |
| if promotion_module | |
| promotion_module.update_from_json(module_json, position) | |
| else | |
| promotion_module = promotion_modules.create_from_json(module_json, position) | |
| end | |
| problems << promotion_module.errors.full_messages.to_sentence unless promotion_module.valid? | |
| end | |
| problems | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment