Class: BackboneGenerator::ModelGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/backbone_generator/model_generator.rb

Instance Method Summary collapse

Instance Method Details

#collection_name(classify = false) ⇒ Object



70
71
# File 'lib/generators/backbone_generator/model_generator.rb', line 70

def collection_name(classify=false)
end

#create_modelObject



10
11
12
13
# File 'lib/generators/backbone_generator/model_generator.rb', line 10

def create_model
  destination_dir = File.join( ['app/assets/javascripts', namespace, "models/#{model_name}.coffee"].compact )
  template 'app/assets/javascripts/%namespace%/models/%model_name%.coffee.tt', destination_dir
end

#create_specsObject



15
16
17
18
19
20
21
# File 'lib/generators/backbone_generator/model_generator.rb', line 15

def create_specs
  destination_dir = File.join( ['spec/javascripts', namespace].compact )
  spec_paths = ['factories/%model_name%_factory.coffee', 'models/%model_name%_spec.coffee']
  spec_paths.each do |path|
    template "spec/javascripts/%namespace%/#{path}.tt", "spec/javascripts/%namespace%/#{path}"
  end
end

#model_name(classify = false) ⇒ Object

def namespace(classify=false)

style      = classify ?  :camelize : :underscore
join_style = classify ? '.' : '/'

_namespaces = raw_collection_name.split('::')
_collection_name = _namespaces.pop
unless _namespaces.empty?
  _namespaces = _namespaces.map {|ns| ns.singularize.send(style)}
end
_namespaces.join(join_style)

end



64
65
66
67
68
# File 'lib/generators/backbone_generator/model_generator.rb', line 64

def model_name(classify=false)
  @model_name, @namespace = raw_model_name.split('::').reverse
  style = classify ?  :camelize : :underscore
  @model_name.singularize.send(style)
end

#namespace(classify = false) ⇒ Object

Helpers



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/generators/backbone_generator/model_generator.rb', line 40

def namespace(classify=false)
  style      = classify ?  :camelize : :underscore
  join_style = classify ? '.' : '/'
  
  _namespaces = raw_model_name.split('::')
  @model_name = _namespaces.pop
  unless _namespaces.empty?
     _namespaces = _namespaces.map {|ns| ns.singularize.send(style)}
     @namespace = _namespaces.join(join_style)
  end
end


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/generators/backbone_generator/model_generator.rb', line 23

def print_tree
  tree =  <<-TREE

    app/assets/javascripts/dashboard
    └── models
        └── widget.coffee

    spec/javascripts/dashboard
    ├── factories
    │   └── widget_factory.coffee
    └── models
        └── widget_spec.coffee
  TREE
  say tree.gsub(/\/dashboard/, (namespace && "/#{namespace}").to_s).gsub(/widget/, model_name)
end