Class: Pwb::Website

Inherits:
ApplicationRecord show all
Extended by:
ActiveHash::Associations::ActiveRecordExtensions
Includes:
FlagShihTzu
Defined in:
app/models/pwb/website.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.unique_instanceObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/pwb/website.rb', line 24

def self.unique_instance
  # there will be only one row, and its ID must be '1'

  # TODO: - memoize
  find(1)
rescue ActiveRecord::RecordNotFound
  # slight race condition here, but it will only happen once
  row = Website.new
  row.id = 1
  row.save!
  row
end

Instance Method Details

These are a list of links for pages to be displayed in the pages section of the admin site TODO: feb 2018 - enable management of below via /config path



51
52
53
54
55
56
57
58
# File 'app/models/pwb/website.rb', line 51

def admin_page_links
  #
  if configuration["admin_page_links"].present?
    configuration["admin_page_links"]
  else
    update_admin_page_links
  end
end

#as_json(options = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/pwb/website.rb', line 89

def as_json(options = nil)
  super({only: [
           "company_display_name", "theme_name",
           "default_area_unit", "default_client_locale",
           "available_currencies", "default_currency",
           "supported_locales", "social_media",
           "raw_css", "analytics_id", "analytics_id_type",
           "sale_price_options_from", "sale_price_options_till",
           "rent_price_options_from", "rent_price_options_till"
         ],
         methods: ["style_variables", "admin_page_links"]}.merge(options || {}))
end

#as_json_for_page(options = nil) ⇒ Object



82
83
84
85
86
87
# File 'app/models/pwb/website.rb', line 82

def as_json_for_page(options = nil)
  # Sends data to admin in format compatible
  # with client side page model
  as_json({only: [],
           methods: ["slug", "page_parts", "page_contents"]}.merge(options || {}))
end

#body_styleObject



179
180
181
182
183
184
185
# File 'app/models/pwb/website.rb', line 179

def body_style
  body_style = ""
  if style_variables_for_theme["default"] && (style_variables_for_theme["default"]["body_style"] == "siteLayout.boxed")
    body_style = "body-boxed"
  end
  body_style
end

#default_client_locale_to_useObject



120
121
122
123
124
125
126
# File 'app/models/pwb/website.rb', line 120

def default_client_locale_to_use
  locale = default_client_locale || "en-UK"
  if supported_locales && supported_locales.count == 1
    locale = supported_locales.first
  end
  locale.split("-")[0]
end

#get_element_class(element_name) ⇒ Object

below used when rendering to decide which class names to use for which elements



152
153
154
155
156
# File 'app/models/pwb/website.rb', line 152

def get_element_class(element_name)
  style_details = style_variables_for_theme["vic"] || Pwb::PresetStyle.default_values
  style_associations = style_details["associations"] || []
  style_associations[element_name] || ""
end

#get_page_part(page_part_key) ⇒ Object



41
42
43
44
# File 'app/models/pwb/website.rb', line 41

def get_page_part(page_part_key)
  # byebug
  page_parts.where(page_part_key: page_part_key).first
end

#get_style_var(var_name) ⇒ Object

below used by custom stylesheet generator to decide values for various class names (mainly colors)



160
161
162
163
164
# File 'app/models/pwb/website.rb', line 160

def get_style_var(var_name)
  style_details = style_variables_for_theme["vic"] || Pwb::PresetStyle.default_values
  style_vars = style_details["variables"] || []
  style_vars[var_name] || ""
end

#is_multilingualObject



104
105
106
# File 'app/models/pwb/website.rb', line 104

def is_multilingual
  supported_locales.length > 1
end

#logo_urlObject

def custom_css_file

# used by css_controller to decide which file to compile
# with user set variables.
#
custom_css_file = "standard"
# if self.site_template.present? && self.site_template.custom_css_file
#   custom_css_file = self.site_template.custom_css_file
# end
custom_css_file

end



199
200
201
202
203
204
205
206
# File 'app/models/pwb/website.rb', line 199

def logo_url
  logo_url = nil
  logo_content = Content.find_by_key("logo")
  if logo_content && !logo_content.content_photos.empty?
    logo_url = logo_content.content_photos.first.image_url
  end
  logo_url
end

#page_partsObject



37
38
39
# File 'app/models/pwb/website.rb', line 37

def page_parts
  Pwb::PagePart.where(page_slug: "website")
end

#render_google_analyticsObject



216
217
218
# File 'app/models/pwb/website.rb', line 216

def render_google_analytics
  Rails.env.production? && analytics_id.present?
end

#slugObject



76
77
78
79
80
# File 'app/models/pwb/website.rb', line 76

def slug
  # slug is needed for
  # admin client side page model
  "website"
end

#style_settings=(style_settings) ⇒ Object

allow direct bulk setting of styles from admin UI



167
168
169
# File 'app/models/pwb/website.rb', line 167

def style_settings=(style_settings)
  style_variables_for_theme["vic"] = style_settings
end

#style_settings_from_preset=(preset_style_name) ⇒ Object

allow setting of styles to a preset config from admin UI



172
173
174
175
176
177
# File 'app/models/pwb/website.rb', line 172

def style_settings_from_preset=(preset_style_name)
  preset_style = Pwb::PresetStyle.where(name: preset_style_name).first
  if preset_style
    style_variables_for_theme["vic"] = preset_style.attributes.as_json
  end
end

#style_variablesObject

admin client & default.css.erb uses style_variables but it is stored in style_variables_for_theme json col In theory, could have different style_variables per theme but not doing that right now



132
133
134
135
136
137
138
139
140
141
# File 'app/models/pwb/website.rb', line 132

def style_variables
  default_style_variables = {
    "primary_color" => "#e91b23", # red
    "secondary_color" => "#3498db", # blue
    "action_color" => "green",
    "body_style" => "siteLayout.wide",
    "theme" => "light"
  }
  style_variables_for_theme["default"] || default_style_variables
end

#style_variables=(style_variables) ⇒ Object



143
144
145
# File 'app/models/pwb/website.rb', line 143

def style_variables=(style_variables)
  style_variables_for_theme["default"] = style_variables
end

#supported_locales_with_variantsObject



108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/pwb/website.rb', line 108

def supported_locales_with_variants
  supported_locales_with_variants = []
  supported_locales.each do |supported_locale|
    slwv_array = supported_locale.split("-")
    locale = slwv_array[0] || "en"
    variant = slwv_array[1] || slwv_array[0] || "UK"
    slwv = { "locale" => locale, "variant" => variant.downcase }
    supported_locales_with_variants.push slwv
  end
  supported_locales_with_variants
end

#theme_name=(theme_name_value) ⇒ Object



208
209
210
211
212
213
214
# File 'app/models/pwb/website.rb', line 208

def theme_name=(theme_name_value)
  theme_with_name_exists = Pwb::Theme.where(name: theme_name_value).count > 0
  if theme_with_name_exists
    write_attribute(:theme_name, theme_name_value)
    # this is same as self[:theme_name] = theme_name_value
  end
end

TODO: - call this each time a page needs to be added or deleted from admin jan 2018 - currently if a link title is updated the admin_page_links cached in configuration do not get updated



66
67
68
69
70
71
72
73
74
# File 'app/models/pwb/website.rb', line 66

def update_admin_page_links
  admin_page_links = []
  Pwb::Link.ordered_visible_admin.each do |link|
    admin_page_links.push link.as_json
  end
  configuration["admin_page_links"] = admin_page_links
  save!
  admin_page_links
end