Class: TrustyCms::Extension

Inherits:
Object
  • Object
show all
Includes:
Annotatable, Simpleton
Defined in:
lib/trusty_cms/extension.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Annotatable

included

Methods included from Simpleton

included

Instance Attribute Details

#active=(value) ⇒ Object (writeonly)

Sets the attribute active

Parameters:

  • value

    the value to set the attribute active to.



12
13
14
# File 'lib/trusty_cms/extension.rb', line 12

def active=(value)
  @active = value
end

Class Method Details

.activate_extensionObject



105
106
107
108
109
110
111
112
# File 'lib/trusty_cms/extension.rb', line 105

def activate_extension
  return if instance.active?

  instance.activate_extension if instance.respond_to? :activate
  Dir["#{Rails.root}/config/routes/**/*.rb"].each do |route_file|
  end
  instance.active = true
end

.deactivate_extensionObject Also known as: deactivate



114
115
116
117
118
119
# File 'lib/trusty_cms/extension.rb', line 114

def deactivate_extension
  return unless instance.active?

  instance.active = false
  instance.deactivate if instance.respond_to? :deactivate
end

.extension_config {|Rails.configuration| ... } ⇒ Object

Yields:



130
131
132
# File 'lib/trusty_cms/extension.rb', line 130

def extension_config
  yield Rails.configuration
end

.inherited(subclass) ⇒ Object



122
123
124
# File 'lib/trusty_cms/extension.rb', line 122

def inherited(subclass)
  subclass.extension_name = subclass.name.to_name('Extension')
end

.migrate_from(extension_name, until_migration = nil) ⇒ Object



126
127
128
# File 'lib/trusty_cms/extension.rb', line 126

def migrate_from(extension_name, until_migration = nil)
  instance.migrates_from[extension_name] = until_migration
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/trusty_cms/extension.rb', line 14

def active?
  @active
end

#add_item(*args) ⇒ Object



88
89
90
# File 'lib/trusty_cms/extension.rb', line 88

def add_item(*args)
  @the_tab.add_item(*args)
end

#adminObject



61
62
63
# File 'lib/trusty_cms/extension.rb', line 61

def admin
  AdminUI.instance
end

#enabled?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/trusty_cms/extension.rb', line 26

def enabled?
  active? and migrated?
end

#extension_enabled?(extension) ⇒ Boolean

Determine if another extension is installed and up to date.

if MyExtension.extension_enabled?(:third_party)

ThirdPartyExtension.extend(MyExtension::IntegrationPoints)

end

Returns:

  • (Boolean)


97
98
99
100
101
102
# File 'lib/trusty_cms/extension.rb', line 97

def extension_enabled?(extension)
  extension = (extension.to_s.camelcase + 'Extension').constantize
  extension.enabled?
rescue NameError
  false
end

#load_initializersObject



47
48
49
50
51
# File 'lib/trusty_cms/extension.rb', line 47

def load_initializers
  Dir["#{root}/config/initializers/**/*.rb"].sort.each do |initializer|
    require initializer
  end
end

#migrated?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/trusty_cms/extension.rb', line 22

def migrated?
  migrator.new(:up, migrations_path).pending_migrations.empty?
end

#migrates_fromObject



39
40
41
# File 'lib/trusty_cms/extension.rb', line 39

def migrates_from
  @migrates_from ||= {}
end

#migrations_pathObject



35
36
37
# File 'lib/trusty_cms/extension.rb', line 35

def migrations_path
  File.join(root, 'db', 'migrate')
end

#migratorObject



53
54
55
56
57
58
59
# File 'lib/trusty_cms/extension.rb', line 53

def migrator
  unless @migrator
    extension = self
    @migrator = Class.new(ExtensionMigrator) { self.extension = extension }
  end
  @migrator
end

#rootObject



18
19
20
# File 'lib/trusty_cms/extension.rb', line 18

def root
  path.to_s
end

#routed?Boolean

Conventional plugin-like routing

Returns:

  • (Boolean)


31
32
33
# File 'lib/trusty_cms/extension.rb', line 31

def routed?
  File.exist?(routing_file)
end

#routing_fileObject



43
44
45
# File 'lib/trusty_cms/extension.rb', line 43

def routing_file
  File.join(root, 'config', 'routes.rb')
end

#tab(name, options = {}, &block) ⇒ Object Also known as: add_tab



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/trusty_cms/extension.rb', line 65

def tab(name, options = {}, &block)
  @the_tab = admin.nav[name]
  unless @the_tab
    @the_tab = TrustyCms::AdminUI::NavTab.new(name)
    before = options.delete(:before)
    after = options.delete(:after)
    tab_name = before || after
    tab_object = admin.nav[tab_name]
    if tab_object
      index = admin.nav.index(tab_object)
      index += 1 unless before
      admin.nav.insert(index, @the_tab)
    else
      admin.nav << @the_tab
    end
  end
  if block_given?
    block.call(@the_tab)
  end
  @the_tab
end