File: model_generator.rb

package info (click to toggle)
ruby-activeldap 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,588 kB
  • sloc: ruby: 18,143; sh: 12; makefile: 5
file content (47 lines) | stat: -rw-r--r-- 1,534 bytes parent folder | download | duplicates (4)
1
2
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
require 'rails/generators'
require 'active_ldap'

module ActiveLdap
  module Generators
    class ModelGenerator < Rails::Generators::NamedBase
      include ActiveLdap::GetTextSupport
      source_root File.expand_path('../templates', __FILE__)
      
      class_option :dn_attribute, :type => :string, :default => 'cn',
        :desc => _("Use ATTRIBUTE as default DN attribute for " \
                   "instances of this model")
      class_option :prefix, :type => :string,
        :desc => _("Use PREFIX as prefix for this model")
      class_option :classes, :type => :array, :default => nil,
        :desc => _("Use CLASSES as required objectClass for instances of this model")
      
      def create_model
        template 'model_active_ldap.rb', File.join('app/models', class_path, "#{file_name}.rb")
      end
      
      hook_for :test_framework, :as => :model
      
      private
      
      def prefix
        options[:prefix] || default_prefix
      end
      
      def default_prefix
        "ou=#{name.demodulize.pluralize}"
      end
      
      def ldap_mapping(indent='  ')
        mapping = "ldap_mapping "
        mapping_options = ["dn_attribute: #{options[:dn_attribute].dump}"]
        mapping_options << "prefix: #{prefix.dump}"
        if options[:classes]
          mapping_options << "classes: #{options[:classes].inspect}"
        end
        mapping_options = mapping_options.join(",\n#{indent}#{' ' * mapping.size}")
        "#{indent}#{mapping}#{mapping_options}"
      end
    end
  end
end