Sha256: 5c677f2eba42c6ecb163bfa16fc82495e9f377ad8785076b60d752f45bf2aef3

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

module Spina
  class Account < ActiveRecord::Base
    serialize :preferences
    include Spina::Partable

    mount_uploader :logo, LogoUploader

    has_many :layout_parts, dependent: :destroy
    accepts_nested_attributes_for :layout_parts, allow_destroy: true

    alias_attribute :layout_part, :part
    alias_attribute :parts, :layout_parts

    after_save :bootstrap_website

    def to_s
      name
    end

    def content(layout_part)
      layout_part = layout_parts.where(name: layout_part).first
      layout_part.try(:content)
    end

    def self.serialized_attr_accessor(*args)
      args.each do |method_name|
        eval "
          def #{method_name}
            (self.preferences || {})[:#{method_name}]
          end

          def #{method_name}=(value)
            self.preferences ||= {}
            self.preferences[:#{method_name}] = value
          end
        "
      end
    end

    serialized_attr_accessor :google_analytics, :google_site_verification, :facebook, :twitter, :google_plus, :theme, :aviary_api_key, :aviary_language, :ngrok_address

    private

    def bootstrap_website
      theme = ::Spina.theme(self.theme)
      bootstrap_pages(theme) if theme
    end

    def bootstrap_pages(theme)
      find_or_create_custom_pages(theme)
      deactivate_unused_view_templates(theme)
      activate_used_view_templates(theme)
    end

    def find_or_create_custom_pages(theme)
      theme.config.custom_pages.each do |page|
        Page.where(name: page[:name]).first_or_create(title: page[:title]).update_attributes(view_template: page[:view_template], deletable: page[:deletable])
      end
    end

    def deactivate_unused_view_templates(theme)
      Page.where.not(view_template: theme.config.view_templates.keys).update_all(active: false)
    end

    def activate_used_view_templates(theme)
      Page.where(view_template: theme.config.view_templates.keys).update_all(active: true)
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spina-0.8.1 app/models/spina/account.rb
spina-0.8.0 app/models/spina/account.rb
spina-0.7.3 app/models/spina/account.rb