Class: Berlin::Engine

Inherits:
Rails::Engine
  • Object
show all
Defined in:
lib/berlin.rb

Class Method Summary collapse

Class Method Details

.activateObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/berlin.rb', line 10

def self.activate
  
  # require language helper
  #require 'berlin_helper'
  
  Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
    Rails.env.production? ? require(c) : load(c)
  end

  # add config value for disabling shopping functions
  AppConfiguration::preference :disable_shop, :boolean, :default => false
  
  # for Admin
  Admin::BaseController.class_eval do
    helper BerlinHelper, LanguageHelper
  end
  
  # for Frontend
  Spree::BaseController.class_eval do
    helper BerlinHelper, LanguageHelper, ProductsHelper
    before_filter :set_language

    def set_language
      if params['locale']
        I18n.locale = params[:locale]
      elsif session[:locale_current]
        # when function is called, session.locale is already set,
        # so had to use an alterative var (locale_current)
        I18n.locale = session[:locale_current]
      end
      @locale = I18n.locale
      session[:locale_current] = I18n.locale.to_s
    end
  end
  
  ActiveRecord::Base.class_eval do
    
    # set active user
    before_create :set_created_by
    before_save :set_updated_by
    
    def created_by_user
      if !self.nil? && self.respond_to?(:created_by) && !self.created_by.nil?
        return User.find(self.created_by)
      end
      return false
    end
    
    def updated_by_user
      if !self.nil? && self.respond_to?(:updated_by) && !self.updated_by.nil?
        return User.find(self.updated_by)
      end
      return false
    end
    
    private
      def set_created_by
        self[:created_by] = UserSession.find.user.id if !self.nil? && self.respond_to?(:created_by)
      end

      def set_updated_by
        self[:updated_by] = UserSession.find.user.id if !self.nil? && self.respond_to?(:updated_by)
      end
      
  end
  
  # only extend, when tables exists
  # otherwise, error (only on migrating)
  if ActiveRecord::Base.connection.tables.include?('products')
    
    # add content translations using Globalize 3's helpers        
    Product.class_eval do
      translates :name, :description, :meta_description, :meta_keywords
    end

    Property.class_eval do
      translates :presentation
    end

    Prototype.class_eval do
      translates :name
    end

    Taxonomy.class_eval do
      translates :name
    end

    Taxon.class_eval do
      translates :name
    end

    OptionType.class_eval do
      translates :presentation
    end

    OptionValue.class_eval do
      translates :presentation
    end
  
  end
  
  # Enable I18n fallbacks
  require "i18n/backend/fallbacks" 

end