Class: BackboneGenerator::CollectionGenerator

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

Instance Method Summary collapse

Instance Method Details

#collection_base_classObject

Use the extended class if availiable ( recommended )



56
57
58
# File 'lib/generators/backbone_generator/collection_generator.rb', line 56

def collection_base_class
  File.exists?(Rails.root.join('app/assets/javascripts/shared/core_extentions/collections_extentions.coffee')) ? 'Backbone.ExtendedCollection' : 'Backbone.Collection'
end

#collection_name(classify = false) ⇒ Object

Helpers



49
50
51
52
53
# File 'lib/generators/backbone_generator/collection_generator.rb', line 49

def collection_name(classify=false)
  style = classify ?  :camelize : :underscore
  _collection_name, _namespace = raw_collection_name.split('::').reverse
  _collection_name.pluralize.send(style)
end

#create_collectionObject



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

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

#create_modelObject



15
16
17
18
# File 'lib/generators/backbone_generator/collection_generator.rb', line 15

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



20
21
22
23
24
25
26
# File 'lib/generators/backbone_generator/collection_generator.rb', line 20

def create_specs
  destination_dir = File.join( ['spec/javascripts', namespace].compact )
  spec_paths = ['collections/%collection_name%_spec.coffee', '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



72
73
74
# File 'lib/generators/backbone_generator/collection_generator.rb', line 72

def model_name(classify=false)
  collection_name(classify).singularize
end

#namespace(classify = false) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/generators/backbone_generator/collection_generator.rb', line 60

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


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/generators/backbone_generator/collection_generator.rb', line 28

def print_tree
  tree =  <<-TREE

    app/assets/javascripts/dashboard
    ├── collections
    │   └── widgets.coffee
    └── models
        └── widget.coffee

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