Class: Rails::Generators::LeoscaControllerGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_seeds_dbObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/generators/rails/leosca_controller/leosca_controller_generator.rb', line 77

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{|attribute| items << attribute_to_hash(attribute)}
    row = "{ #{items.join(', ')} }"

    #TODO: to have different values for every row
    content = "#{CRLF}### Created by leosca controller generator ### #{CRLF}"
    attributes.each{|attribute| content << attribute_to_range(attribute)}
    if /^3./ === Rails.version
      content << attributes_accessible(attributes, class_name)
    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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/generators/rails/leosca_controller/leosca_controller_generator.rb', line 100

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
    <<-FILE.gsub(/^          /, '')

      index do
        selectable_column
        id_column
    #{attributes.first(5).map{|attr| "    column(:#{attr.name}){|#{singular_table_name}| #{singular_table_name}.#{attr.name}}"}.join("\n")}
        actions
      end

      show do |#{singular_table_name}|
        attributes_table do
    #{attributes.map{|attr| "      row(:#{attr.name}){|#{singular_table_name}| #{singular_table_name}.#{attr.name}}"}.join("\n")}
          row :created_at
          row :updated_at
        end
        # Insert here child tables
        #panel I18n.t('models.xxxxx') do
        #  table_for #{singular_table_name}.xxxxx do
        #    column(:id) {|xxxxx| link_to xxxxx.id, [:admin, xxxxx]}
        #end
        active_admin_comments
      end

    #{attributes.map{|attr| "  filter :#{attr.name}"}.join("\n")}

      form do |f|
        f.inputs do
    #{attributes.map{|attr| "      f.input :#{attr.name}"}.join("\n")}
        end
        #For date use                    as: :datepicker, input_html: { class: 'calendar' }
        #For state machine data field    as: :select, collection: (f.object.class.state_machine.states.collect { |state| [state.human_name.underscore.capitalize, state.value] }.sort_by { |name| name }), :for => :states, :include_blank => false
        f.actions
      end
    FILE
  end if File.exists?(file)

  if /^[4-5]/ === Rails.version
    inject_into_file file, :after => "ActiveAdmin.register #{class_name} do" do
      <<-FILE.gsub(/^            /, '')

        permit_params do
          permitted = [:id, #{attributes.map{|attr| ":#{attr.name}"}.join(', ')}, :created_at, :updated_at]
          permitted
        end

      FILE
    end if File.exists?(file)
  end

  inject_into_file file, :after => "ActiveAdmin.register #{class_name} do" do
    <<-FILE.gsub(/^          /, '')

      menu :if => proc{ can?(:read, #{class_name}) }

      controller do
        load_resource :except => :index
      end

    FILE
  end if authorization? && File.exists?(file)

end

#update_ability_modelObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/generators/rails/leosca_controller/leosca_controller_generator.rb', line 64

def update_ability_model
  return unless authorization?
  inject_into_file authorization_file, :before => "  end\nend" do
    <<-FILE.gsub(/^      /, '')

    # ----- #{class_name.upcase} ----- #
    can :read, #{class_name}                                if #{options[:auth_class].downcase}.role? :guest
    can [:read, :create, :update], #{class_name}            if #{options[:auth_class].downcase}.role? :user
    can [:read, :create, :update, :destroy], #{class_name}  if #{options[:auth_class].downcase}.role? :manager
    FILE
  end
end

#update_specsObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/generators/rails/leosca_controller/leosca_controller_generator.rb', line 170

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

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
# 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
      attributes_to_list(attributes, file_name)
    end

    #Model name
    inject_into_file file, :after => "models: &models" do
      <<-FILE.gsub(/^      /, '')

      #{file_name}: "#{file_name.capitalize}"
      #{controller_name}: "#{controller_name.capitalize}"
      FILE
    end

    #Formtastic
    inject_into_file file, :after => "    hints:" do
      attributes_to_hints(attributes, file_name)
    end

  end
end