Class: Rails::Generators::LeoscaControllerGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_seeds_dbObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/generators/rails/leosca_controller/leosca_controller_generator.rb', line 108

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}"
    attributes.each do |attribute|
      content << attribute_to_range(attribute)
    end
    content << "#{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



27
28
29
# File 'lib/generators/rails/leosca_controller/leosca_controller_generator.rb', line 27

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

#invoke_active_adminObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/generators/rails/leosca_controller/leosca_controller_generator.rb', line 132

def invoke_active_admin
  return unless activeadmin? and options[:activeadmin]
  #Rails::Generators.invoke("active_admin:resource", [singular_table_name])
  invoke "active_admin:resource", [singular_table_name]
  file = "app/admin/#{singular_table_name}.rb"

  inject_into_file file, :after => "ActiveAdmin.register #{class_name} do" do
    "\n      menu :if => proc{ can?(:read, \#{class_name}) }\n\n      permit_params do\n        permitted = [\#{attributes.map{|attr| \":\#{attr.name}\"}.join(', ')}]\n        permitted\n      end\n\n      controller do\n        load_resource :except => :index\n      end\n\n\n    FILE\n  end if authorization? && File.exists?(file)\nend\n".gsub(/^          /, '')

#update_ability_modelObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/generators/rails/leosca_controller/leosca_controller_generator.rb', line 89

def update_ability_model
  return unless authorization?
  inject_into_file authorization_file, :before => "  end\nend" do
    "\n    # ----- \#{class_name.upcase} ----- #\n    can :read, \#{class_name} if \#{options[:auth_class].downcase}.role? :guest\n    if \#{options[:auth_class].downcase}.role? :user\n      can [:read, :create], \#{class_name}\n      can [:update, :destroy], \#{class_name}\n    end\n    if \#{options[:auth_class].downcase}.role? :manager\n      can [:read, :create, :update, :destroy], \#{class_name}\n    end\n\n    FILE\n  end\nend\n".gsub(/^      /, '')

#update_specsObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/generators/rails/leosca_controller/leosca_controller_generator.rb', line 157

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
    "\n    factory :\#{singular_table_name} do |\#{singular_table_name[0..0]}|\n  \#{items.join(CRLF)}\n    end\n    FILE\n  end if File.exists?(file)\nend\n".gsub(/^        /, '')

#update_yaml_localesObject

Override hook_for :template_engine, :test_framework, :as => :leosca



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

def update_yaml_locales
  #Inject model and attributes name into yaml files for i18n
  path = "config/locales"
  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" do
      content = "#{CRLF}      #{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}"
      #content << "        op_index: \"Listing #{plural_table_name}\"#{CRLF}"
      content
    end

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