Class: Rails::Generators::LeoscaControllerGenerator

Inherits:
ScaffoldControllerGenerator
  • Object
show all
Includes:
Leonardo::Leosca, Leonardo::Nested, Leonardo::Nested::Test
Defined in:
lib/generators/rails/leosca_controller/leosca_controller_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_seeds_dbObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/generators/rails/leosca_controller/leosca_controller_generator.rb', line 121

def add_seeds_db
  return unless options.seeds? and options[:seeds_elements].to_i > 0
  file = "db/seeds.rb"
  append_file file do
    items = []
    attributes.each do |attribute|
      items << attribute_to_hash(attribute)
    end
    row = "{ #{items.join(', ')} }"

    #TODO: to have different values for every row
    content = "#{CRLF}### Created by leosca controller generator ### #{CRLF}#{class_name}.create([#{CRLF}"
    options[:seeds_elements].to_i.times do |n|
      content << "#{row.gsub(/\#/, (n+1).to_s)},#{CRLF}"
    end
    content << "])#{CRLF}"
    content
  end if File.exists?(file)
end

#create_controller_filesObject

Override



25
26
27
# File 'lib/generators/rails/leosca_controller/leosca_controller_generator.rb', line 25

def create_controller_files
  template 'controller.rb', File.join('app/controllers', class_path, base_namespaces, "#{controller_file_name}_controller.rb")
end

#update_ability_modelObject



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
# File 'lib/generators/rails/leosca_controller/leosca_controller_generator.rb', line 91

def update_ability_model
  file = "app/models/ability.rb"
  return unless File.exists?(file)
  inject_into_file file, :before => "  end\nend" do
    <<-FILE.gsub(/^      /, '')
    #can :read, #{class_name} if #{options[:auth_class]}.new_record? #Guest
    can :read, #{class_name} if #{options[:auth_class]}.role? :guest #Registered guest
    if #{options[:auth_class]}.role? :user
      can :read, #{class_name}
      can :create, #{class_name}
      can [:update, :destroy, :select, :create_multiple, :update_multiple], #{class_name} do |#{singular_table_name}|
        if defined?(#{singular_table_name}.#{options[:auth_class]}_id)
          #{singular_table_name}.#{options[:auth_class]}_id == #{options[:auth_class]}.id
        else
          true
        end
      end
    end
    if #{options[:auth_class]}.role? :manager
      can :read, #{class_name}
      can :update, #{class_name}
      can :create, #{class_name}
      can :destroy, #{class_name}
      can [:select, :create_multiple, :update_multiple], #{class_name}
    end

    FILE
  end
end

#update_specsObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/generators/rails/leosca_controller/leosca_controller_generator.rb', line 141

def update_specs
  file = "spec/spec_helper.rb"
  return unless File.exists? file

  file = "spec/factories.rb"
  inject_into_file file, :before => "  ### Insert below here other your factories ###" do
    items = []
    attributes.each do |attribute|
      items << attribute_to_factories(attribute)
    end
    <<-FILE.gsub(/^        /, '')

    factory :#{singular_table_name} do |#{singular_table_name[0..0]}|
  #{items.join(CRLF)}
    end
    FILE
  end if File.exists?(file)

end

#update_yaml_localesObject



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
# File 'lib/generators/rails/leosca_controller/leosca_controller_generator.rb', line 32

def update_yaml_locales
  #Inject model and attributes name into yaml files for i18n
  path = "config/locales"
  files = []
  files = Dir["#{path}/??.yml"]
  files.each do |file|

    next unless File.exists?(file)

    #Fields name
    inject_into_file file, :after => "#Attributes zone - do not remove#{CRLF}" do
      content = "      #{file_name}:#{CRLF}"
      attributes.each do |attribute|
        content << "        #{attribute.name}: \"#{attribute.name.humanize}\"#{CRLF}"
      end
      content << "        op_new: \"New #{singular_table_name}\"#{CRLF}"
      content << "        op_edit: \"Editing #{singular_table_name}\"#{CRLF}"
      content << "        op_edit_multiple: \"Editing #{plural_table_name}\"#{CRLF}"
      content << "        op_copy: \"Creating new #{plural_table_name}\"#{CRLF}"
      if nested?
        content << "        op_index: \"Listing #{plural_table_name} belongings to %{parent} %{name}\"#{CRLF}"
      else
        content << "        op_index: \"Listing #{plural_table_name}\"#{CRLF}"
      end
      content
    end

    #Model name
    inject_into_file file, :after => "models: &models#{CRLF}" do
      <<-FILE.gsub(/^      /, '')
      #{file_name}: "#{file_name.capitalize}"
      #{controller_name}: "#{controller_name.capitalize}"
      FILE
    end

    #Formtastic
    inject_into_file file, :after => "    hints:#{CRLF}" do
      content = "      #{file_name}:#{CRLF}"
      attributes.each do |attribute|
        attr_name = attribute.name.humanize
        case attribute.type
        when :integer, :decimal, :float
          content << "        #{attribute.name}: \"Fill the #{attr_name} with a#{"n" if attribute.type == :integer} #{attribute.type.to_s} number\"#{CRLF}"
        when :boolean
          content << "        #{attribute.name}: \"Select if this #{file_name} should be #{attr_name} or not\"#{CRLF}"
        when :string, :text
          content << "        #{attribute.name}: \"Choose a good #{attr_name} for this #{file_name}\"#{CRLF}"
        when :date, :datetime, :time, :timestamp
          content << "        #{attribute.name}: \"Choose a #{attribute.type.to_s} for #{attr_name}\"#{CRLF}"
        else
          content << "        #{attribute.name}: \"Choose a #{attr_name}\"#{CRLF}"
        end
      end
      content
    end

  end
end