Class: InlineForms::InlineFormsGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/inline_forms_generator.rb

Overview

Usage

This generator generates a migration, a model and a controller.

rails g inline_forms Model attribute:type [attribute:type ...] [options]

Read more about the possible types below.

Overriding Rails::Generators::GeneratedAttribute

When using a generator in the form

rails g example_generator Modelname attribute:type attribute:type ...

an array with attributes and types is created for use in the generator.

Rails::Generators::GeneratedAttribute creates, among others, an attribute_type. This attribute_type maps column types to form attribute helpers like text_field. We override it in ‘lib/generators/inline_forms_attribute_overrides.rb` (shared with `InlineFormsAddtoGenerator`).

Instance Method Summary collapse

Instance Method Details

#add_second_top_barObject



181
182
183
# File 'lib/generators/inline_forms_generator.rb', line 181

def add_second_top_bar
  copy_file "_inline_forms_tabs.html.erb", "app/views/_inline_forms_tabs.html.erb" unless File.exist?('app/views/_inline_forms_tabs.html.erb')
end

#add_tabObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/generators/inline_forms_generator.rb', line 185

def add_tab
  return if @flag_not_accessible_through_html

  # The installer seeds `MODEL_TABS = %w(<user_model_route> )` in
  # `config/initializers/inline_forms.rb` before any `rails g inline_forms`
  # runs. Inject each generated model's pluralised route token into that
  # list so the inline_forms top-bar dropdown (rendered by
  # `app/views/inline_forms/_header.html.erb`) surfaces every
  # HTML-reachable model. No-op when the initializer is absent (e.g. a
  # consumer running `rails g inline_forms` in a non-installer-shaped app)
  # or when the token is already present (idempotent re-run).
  relative_path = "config/initializers/inline_forms.rb"
  marker = "MODEL_TABS = %w("
  tab_token = "#{name.pluralize.underscore} "
  full_path = File.join(destination_root, relative_path)
  return unless File.exist?(full_path)

  content = File.read(full_path)
  return unless content.include?(marker)
  return if content.include?(tab_token.rstrip)

  inject_into_file relative_path, tab_token, after: marker
end

#generate_controllerObject



213
214
215
# File 'lib/generators/inline_forms_generator.rb', line 213

def generate_controller
  template "controller.erb", "app/controllers/#{controller_file_name}.rb" if @flag_create_controller
end

#generate_migrationObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/generators/inline_forms_generator.rb', line 154

def generate_migration
  if @flag_create_migration
    @columns = String.new

    for attribute in attributes
      if attribute.column_type == :image
        @columns << '      t.string    :' + attribute.name + "_file_name\n"
        @columns << '        t.string    :' + attribute.name + "_content_type\n"
        @columns << '        t.integer   :' + attribute.name + "_file_size\n"
        @columns << '        t.datetime  :' + attribute.name + "_updated_at\n"
      else
        if attribute.migration?
          attribute.attribute_type == :unknown ? commenter = '#' : commenter = ' '
          @columns << commenter +
            '     t.' +
            attribute.column_type.to_s +
            " :" +
            attribute.name +
            " \n"
        end
      end
    end
    @primary_key_option = @create_id ? '' : ', id: false'
    template "migration.erb", "db/migrate/#{time_stamp}_inline_forms_create_#{table_name}.rb"
  end
end

#generate_modelObject



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
90
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/generators/inline_forms_generator.rb', line 57

def generate_model
  if @flag_create_model
    @belongs_to               = "\n"
    @has_many                 = "\n"
    @has_one                  = "\n"
    @habtm                    = "\n"
    @has_rich_text            = "\n"
    @has_attached_files       = "\n"
    @presentation             = "\n"
    @order                    = "\n"
    @list_scopes              = ""
    @carrierwave_mounters     = "\n"
    @inline_forms_attribute_list  = String.new

    for attribute in attributes
      if attribute.column_type  == :belongs_to
        ## :drop_down, :references and :belongs_to all end up with the column_type :belongs_to
        @belongs_to << '  belongs_to :'         + attribute.name + "\n"
      end
      # upload images or audio files via carrierwave
      case attribute.type when :image_field, :audio_field then
        @carrierwave_mounters << '  mount_uploader :' + attribute.name + ', ' + "#{attribute.name}_uploader".camelcase + "\n"
      end
      if attribute.type         == :has_many ||
          attribute.type         == :has_many_destroy
        @has_many << '  has_many :'             + attribute.name
        @has_many << ', :dependent => :destroy' if attribute.type == :has_many_destroy
        @has_many << "\n"
      end
      if attribute.type         == :has_one
        @has_one << '  has_one :'               + attribute.name + "\n"
      end
      if attribute.type         == :habtm ||
          attribute.type         == :has_and_belongs_to_many ||
          attribute.type         == :check_list
        @habtm << '  has_and_belongs_to_many :' + attribute.name + "\n"
      end
      if attribute.type == :rich_text
        @has_rich_text << '  has_rich_text :' + attribute.name + "\n"
      end
      if attribute.name == '_presentation'
        @presentation <<  "  def _presentation\n" +
          "    \"#{attribute.type.to_s}\"\n" +
          "  end\n" +
          "\n"
      end
      # `_list_order:col` (preferred) and the legacy alias `_order:col`
      # both emit the inline_forms_list scope plus a Ruby `<=>` for
      # in-memory sort (used by check_list show on the association).
      if attribute.name == '_order' || attribute.name == '_list_order'
        if attribute.name == '_order'
          say_status :deprecated,
                     "_order:#{attribute.type} is deprecated; use _list_order:#{attribute.type}",
                     :yellow
        end
        @order <<         "  def <=>(other)\n" +
          "    self.#{attribute.type} <=> other.#{attribute.type}\n" +
          "  end\n" +
          "\n"
        @list_scopes << "  scope :inline_forms_list, -> { order(:#{attribute.type}, :id) }\n"
      end
      if attribute.name == '_list_search'
        @list_scopes << "  scope :inline_forms_search, ->(q) { where(\"#{attribute.type} LIKE ?\", \"%\#{q}%\") }\n"
      end
      if attribute.attribute?
        attribute.attribute_type == :unknown ? commenter = '#' : commenter = ' '
        @inline_forms_attribute_list << commenter +
          '     [ :' +
          attribute.name +
          ', :' + attribute.attribute_type.to_s +
          " ], \n"
      end
    end
    unless @inline_forms_attribute_list.empty?
      @inline_forms_attribute_list =  "\n" +
        "  def inline_forms_attribute_list\n" +
        "    @inline_forms_attribute_list ||= [\n" +
        @inline_forms_attribute_list +
        "    ]\n" +
        "  end\n" +
        "\n"
    end
    template "model.erb", "app/models/#{model_file_name}.rb"
  end
end

#generate_resource_routeObject



143
144
145
146
147
148
149
150
151
152
# File 'lib/generators/inline_forms_generator.rb', line 143

def generate_resource_route
  if @flag_create_resource_route
    route <<-ROUTE.strip_heredoc
      resources :#{resource_name} do
        post 'revert', :on => :member
        get 'list_versions', :on => :member
      end
    ROUTE
  end
end

#generate_testObject



209
210
211
# File 'lib/generators/inline_forms_generator.rb', line 209

def generate_test
  template "test.erb", "test/unit/#{test_file_name}.rb"
end

#set_some_flagsObject

using flags.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/generators/inline_forms_generator.rb', line 29

def set_some_flags
  @flag_not_accessible_through_html   = true
  @flag_create_migration              = true
  @flag_create_model                  = true
  @create_id                          = true
  @unknown_attributes                 = []
  for attribute in attributes
    @flag_not_accessible_through_html = false if attribute.name == '_enabled'
    @flag_create_migration            = false if attribute.name == '_no_migration'
    @flag_create_model                = false if attribute.name == '_no_model'
    @create_id                        = false if attribute.name == "_id" && attribute.type == :false
    if !attribute.name.start_with?('_') && (
        (attribute.attribute? && attribute.attribute_type == :unknown) ||
        (attribute.migration? && attribute.column_type == :unknown)
      )
      @unknown_attributes << "#{attribute.name}:#{attribute.type}"
    end
  end
  allow_unknown = options[:allow_unknown].to_s == 'true'
  if !allow_unknown && !@unknown_attributes.empty?
    raise Thor::Error,
      "Unknown field type(s): #{@unknown_attributes.uniq.join(', ')}. " +
      "Add a valid form element type or pass --allow-unknown to keep legacy commented output."
  end
  @flag_create_controller             = @flag_create_model
  @flag_create_resource_route         = @flag_create_model
end