Class: Intro::Tour

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/intro/tour.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_route(url) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/intro/tour.rb', line 43

def extract_route(url)
  url = "/#{url}" if url !~ /^((https?:\/\/)|(\/))/i
  uri = URI(url)

  {
    source: recognize_path(uri.path),
    query: uri.query
  }
rescue
  {}
end

Instance Method Details

#clear_cacheObject



98
99
100
101
102
# File 'app/models/intro/tour.rb', line 98

def clear_cache
  if destroyed? || (previous_changes.keys & %w[controller_path action_name published]).any?
    Intro.cache.delete(controller_path, action_name)
  end
end

#expired?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/models/intro/tour.rb', line 38

def expired?
  !!expired_at && expired_at < Time.now
end

#expose_attributesObject



20
21
22
# File 'app/models/intro/tour.rb', line 20

def expose_attributes
  as_json(only: %i[id ident controller_path action_name options]).with_indifferent_access
end

#format_attributesObject



74
75
76
77
78
79
80
81
82
# File 'app/models/intro/tour.rb', line 74

def format_attributes
  %i[ident controller_path action_name].each do |attribute|
    strip_attribute = send(attribute).try(:strip)
    send("#{attribute}=", strip_attribute)
  end

  self.options = JSON.parse(options) rescue {} unless options.is_a?(Hash)
  self.route   = JSON.parse(route) rescue {} unless route.is_a?(Hash)
end

#simple_routeObject



24
25
26
# File 'app/models/intro/tour.rb', line 24

def simple_route
  route[:simple] rescue nil
end

#simple_route_changed?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
# File 'app/models/intro/tour.rb', line 28

def simple_route_changed?
  return false unless route_changed?

  simple_route != route_was[:simple] rescue false
end

#strict_route?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'app/models/intro/tour.rb', line 34

def strict_route?
  route[:strict] rescue false
end

#update_routeObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/intro/tour.rb', line 84

def update_route
  if simple_route.present? && simple_route_changed?
    route = self.class.extract_route(simple_route)
    source = route[:source] || {}

    self.controller_path = source[:controller].to_s if self.controller_path.blank?
    self.action_name = source[:action].to_s if self.action_name.blank?
    self.route.reverse_merge!(route)
  end

  self.route[:strict] = %w[1 true].include?(self.route[:strict].to_s)
  self.route
end