Module: Typus

Defined in:
lib/typus.rb,
lib/typus/format.rb,
lib/typus/locale.rb,
lib/typus/reloader.rb,
lib/typus/generator.rb,
lib/typus/quick_edit.rb,
lib/typus/configuration.rb,
lib/typus/authentication.rb

Defined Under Namespace

Modules: Authentication, Configuration, Format, Locale, QuickEdit, Reloader

Class Method Summary collapse

Class Method Details

.application(name) ⇒ Object

Returns a list of the modules of an application.



28
29
30
# File 'lib/typus.rb', line 28

def application(name)
  Typus::Configuration.config.collect { |i| i.first if i.last['application'] == name }.compact.uniq.sort
end

.applicationsObject



21
22
23
# File 'lib/typus.rb', line 21

def applications
  Typus::Configuration.config.collect { |i| i.last['application'] }.compact.uniq.sort
end

.default_localeObject



17
18
19
# File 'lib/typus.rb', line 17

def default_locale
  locales.map(&:last).first
end

.enableObject

Enable application. This is used at boot time.

Typus.enable


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
115
116
117
118
119
120
# File 'lib/typus.rb', line 83

def enable

  # Ruby Extensions
  require 'typus/hash'
  require 'typus/object'
  require 'typus/string'

  # Load configuration and roles.
  Typus::Configuration.config!
  Typus::Configuration.roles!

  # Load translation files from the plugin or the gem.
  if plugin?
    I18n.load_path += Dir[File.join("#{Rails.root}/vendor/plugins/typus/config/locales/**/*.{rb,yml}")]
  else
    Gem.path.each { |g| I18n.load_path += Dir[File.join("#{g}/gems/*typus-#{version}/config/locales/**/*.{rb,yml}")] }
  end

  # Require the test/models on when testing.
  require File.dirname(__FILE__) + '/../test/models' if Typus.testing?

  # Rails Extensions.
  require 'typus/active_record'

  # Mixins.
  require 'typus/authentication'
  require 'typus/format'
  require 'typus/generator'
  require 'typus/locale'
  require 'typus/reloader'
  require 'typus/quick_edit'
  require 'typus/user'

  # Vendor.
  require 'vendor/active_record'
  require 'vendor/paginator'

end

.generatorObject



3
4
5
6
7
8
9
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/typus/generator.rb', line 3

def self.generator

  logger = Logger.new("#{Rails.root}/log/#{Rails.env}.log")

  # Create app/controllers/admin if doesn't exist.
  admin_controllers_folder = "#{Rails.root}/app/controllers/admin"
  Dir.mkdir(admin_controllers_folder) unless File.directory?(admin_controllers_folder)

  # Get a list of all available app/controllers/admin
  admin_controllers = Dir["#{Rails.root}/vendor/plugins/*/app/controllers/admin/*.rb", "#{admin_controllers_folder}/*.rb"]
  admin_controllers = admin_controllers.map { |i| File.basename(i) }

  # Create app/views/admin if doesn't exist.
  admin_views_folder = "#{Rails.root}/app/views/admin"
  Dir.mkdir(admin_views_folder) unless File.directory?(admin_views_folder)

  # Create test/functional/admin if doesn't exist.
  admin_controller_tests_folder = "#{Rails.root}/test/functional/admin"
  if File.directory?("#{Rails.root}/test")
    Dir.mkdir(admin_controller_tests_folder) unless File.directory?(admin_controller_tests_folder)
  end

  # Get a list of all available functional test for admin.
  admin_controller_tests = Dir["#{Rails.root}/vendor/plugins/*/test/functional/admin/*.rb", "#{admin_controller_tests_folder}/*.rb"]
  admin_controller_tests = admin_controller_tests.map { |i| File.basename(i) }

  # Generate unexisting controllers for resources which are not tied to
  # a model.
  resources.each do |resource|

    controller_filename = "#{resource.underscore}_controller.rb"
    controller_location = "#{admin_controllers_folder}/#{controller_filename}"

    if !admin_controllers.include?(controller_filename)
      controller = File.open(controller_location, "w+")
      content = "# Controller generated by Typus, use it to extend admin functionality.\nclass Admin::\#{resource}Controller < TypusController\n\n##\n# This controller was generated because you have defined a resource \n# which is not tied to a model on your <tt>config/typus/XXXXXX_roles.yml</tt> \n# configuration file.\n#\n#     admin:\n#       \#{resource}: index\n#\n\ndef index\nend\n\nend\n    RAW\n\n      controller.puts(content)\n      controller.close\n      logger.info \"=> [typus] Admin::\#{resource}Controller successfully created.\"\n\n    end\n\n    # And now we create the view.\n    view_folder = \"\#{admin_views_folder}/\#{resource.underscore}\"\n    view_filename = \"index.html.erb\"\n\n    if !File.exist?(\"\#{view_folder}/\#{view_filename}\")\n      Dir.mkdir(view_folder) unless File.directory?(view_folder)\n      view = File.open(\"\#{view_folder}/\#{view_filename}\", \"w+\")\n      content = <<-RAW\n<!-- Sidebar -->\n\n<% content_for :sidebar do %>\n<%= typus_block :location => 'dashboard', :partial => 'sidebar' %>\n<% end %>\n\n<!-- Content -->\n\n<h2>\#{resource.humanize}</h2>\n\n<p>And here we do whatever we want to ...</p>\n\n      RAW\n      view.puts(content)\n      view.close\n      logger.info \"=> [typus] app/views/admin/\#{resource.underscore}/index.html.erb successfully created.\"\n    end\n\n  end\n\n  # Generate unexisting controllers for resources which are tied to a \n  # model.\n  models.each do |model|\n\n    # Controller app/controllers/admin/*\n    controller_filename = \"\#{model.tableize}_controller.rb\"\n    controller_location = \"\#{admin_controllers_folder}/\#{controller_filename}\"\n\n    if !admin_controllers.include?(controller_filename)\n      controller = File.open(controller_location, \"w+\")\n\n      content = <<-RAW\n# Controller generated by Typus, use it to extend admin functionality.\nclass Admin::\#{model.pluralize}Controller < Admin::MasterController\n\n=begin\n\n##\n# You can overwrite any Admin::MasterController methods.\n#\ndef index\nend\n\n=end\n\n=begin\n\n##\n# You can extend Admin::MasterController with your methods.\n#\n# This actions have to be defined in <tt>config/typus/application.yml</tt>:\n#\n#   \#{model}:\n#     actions:\n#       index: custom_action\n#       edit: custom_action_for_an_item\n#\n# And you have to add permissions on <tt>config/typus/application_roles.yml</tt> \n# to have access to them.\n#\n#   admin:\n#     \#{model}: create, read, update, destroy, custom_action\n#\n#   editor:\n#     \#{model}: create, read, update, custom_action_for_an_item\n#\n#\ndef custom_action\nend\n\ndef custom_action_for_an_item\nend\n\n=end\n\nend\n      RAW\n\n      controller.puts(content)\n      controller.close\n      logger.info \"=> [typus] Admin::\#{model.pluralize}Controller successfully created.\"\n    end\n\n    # Test test/functional/admin/*_test.rb\n    test_filename = \"\#{model.tableize}_controller_test.rb\"\n    test_location = \"\#{admin_controller_tests_folder}/\#{test_filename}\"\n\n    if !admin_controller_tests.include?(test_filename) && File.directory?(\"\#{Rails.root}/test\")\n      test = File.open(test_location, \"w+\")\n\n      content = <<-RAW\nrequire 'test_helper'\n\nclass Admin::\#{model.pluralize}ControllerTest < ActionController::TestCase\n\n# Replace this with your real tests.\ntest \"the truth\" do\n  assert true\nend\n\nend\n      RAW\n\n      test.puts(content)\n      test.close\n      logger.info \"=> [typus] Admin::\#{model.pluralize}ControllerTest successfully created.\"\n    end\n\n  end\n\nend\n"

.get_model_namesObject



53
54
55
56
# File 'lib/typus.rb', line 53

def get_model_names
  Dir[ "#{Rails.root}/app/models/**/*.rb", 
       "#{Rails.root}/vendor/plugins/**/app/models/**/*.rb" ].collect { |m| File.basename(m).sub(/\.rb$/,'').camelize }
end

.localesObject



13
14
15
# File 'lib/typus.rb', line 13

def locales
  Typus::Configuration.options[:locales]
end

.modelsObject



32
33
34
# File 'lib/typus.rb', line 32

def models
  Typus::Configuration.config.map { |i| i.first }.sort
end

.models_on_headerObject



36
37
38
# File 'lib/typus.rb', line 36

def models_on_header
  models.collect { |m| m if m.constantize.typus_options_for(:on_header) }.compact
end

.module_description(modulo) ⇒ Object



58
59
60
# File 'lib/typus.rb', line 58

def module_description(modulo)
  Typus::Configuration.config[modulo]['description']
end

.pathObject



9
10
11
# File 'lib/typus.rb', line 9

def path
  File.dirname(__FILE__) + '/../'
end

.plugin?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/typus.rb', line 74

def plugin?
  File.exist?("#{Rails.root}/vendor/plugins/typus")
end

.resources(models = get_model_names) ⇒ Object

Return a list of resources, which are models tableless.



43
44
45
46
47
48
49
50
51
# File 'lib/typus.rb', line 43

def resources(models = get_model_names)

  all_resources = Typus::Configuration.roles.keys.map do |key|
                    Typus::Configuration.roles[key].keys
                  end.flatten.sort.uniq

  all_resources.delete_if { |x| models.include?(x) || x == 'TypusUser' } rescue []

end

.testing?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/typus.rb', line 70

def testing?
  Rails.env.test? && Dir.pwd == "#{Rails.root}/vendor/plugins/typus"
end

.user_classObject



62
63
64
# File 'lib/typus.rb', line 62

def user_class
  Typus::Configuration.options[:user_class_name].constantize
end

.user_fkObject



66
67
68
# File 'lib/typus.rb', line 66

def user_fk
  Typus::Configuration.options[:user_fk]
end

.versionObject



5
6
7
# File 'lib/typus.rb', line 5

def version
  @@version ||= File.read("#{path}/VERSION").strip
end