Class: RefinerycmsImageGalleryGenerator

Inherits:
Refinery::Generators::EngineInstaller
  • Object
show all
Defined in:
lib/refinerycms-image-gallery.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RefinerycmsImageGalleryGenerator

Initialize generator with params #########



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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/refinerycms-image-gallery.rb', line 9

def initialize(*args)
  super
  @args = @_initializer.first
  
  @title = @args.first.split(":")
  @name = @title[0]
  @chunk = @title[1]
  @attributes = []
  @column_name = []
  
  # For each param include in migration a new column
  @args.each do |arg|
    unless @args.first == arg
      name = arg.split(":")[0]
      type = arg.split(":")[1]
      @column_name << name
      @attributes << Rails::Generators::GeneratedAttribute.new(name, type)
    end
  end
 
  # Pluralize and camelize the class name
  @plural_name = @name.pluralize
  @plural_class_name = @name.pluralize.camelize
  
  # Check if engine exists
  unless File.directory?("vendor/engines/#{@plural_name}")
    puts "Engine '#{@name}' doesn't exists. Create this engine first."  
    exit
  end

  # Check if migration already exists
  @check_migration = ActiveRecord::Base.connection.table_exists?("#{@plural_name}_images")  

  # Create @attributes variable for new gallery
  if @attributes.empty? && @check_migration
    @columns_exc = ["id", "#{@name}_id", "image_id", "position", "chunk", "created_at", "updated_at"]
    "#{@plural_class_name}Image".constantize.column_names.each_with_index do |name, i|
      type = "#{@plural_class_name}Image".constantize.columns[i].type
      if @columns_exc.index("#{name}").nil?
        @attributes << Rails::Generators::GeneratedAttribute.new(name, type)
      end   
    end
  end

  # Define view class path
  @file_path = "app/views/admin/#{@plural_name}/"
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/refinerycms-image-gallery.rb', line 4

def attributes
  @attributes
end

#file_pathObject

Returns the value of attribute file_path.



4
5
6
# File 'lib/refinerycms-image-gallery.rb', line 4

def file_path
  @file_path
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/refinerycms-image-gallery.rb', line 4

def name
  @name
end

#plural_class_nameObject

Returns the value of attribute plural_class_name.



4
5
6
# File 'lib/refinerycms-image-gallery.rb', line 4

def plural_class_name
  @plural_class_name
end

#plural_nameObject

Returns the value of attribute plural_name.



4
5
6
# File 'lib/refinerycms-image-gallery.rb', line 4

def plural_name
  @plural_name
end

Class Method Details

.next_migration_number(path) ⇒ Object

Create a migration relationsheep beetween model and images #########



62
63
64
# File 'lib/refinerycms-image-gallery.rb', line 62

def self.next_migration_number(path)
  ActiveRecord::Generators::Base.next_migration_number(path)
end

Instance Method Details

#add_js_and_css_to_formObject

Insert into form partial the javascripts and css #########



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/refinerycms-image-gallery.rb', line 201

def add_js_and_css_to_form
  unless @check_migration
    page_options = open("app/views/admin/#{@plural_name}/_form.html.erb").grep(/content_for :javascripts/)
    
    if page_options.empty?
      insert_into_file "app/views/admin/#{@plural_name}/_form.html.erb",
        :before => "<%= form_for" do
          "<% content_for :javascripts do %>" +
            "\n\t<script>" +
              "\n\t\t$(document).ready(function(){" +
                "\n\t\t\tpage_options.init(false, '', '');" +
              "\n\t\t});" +
            "\n\t</script>"+
            "\n\t<%= javascript_include_tag 'gallery' %>" +
          "\n<% end %>" +
        
          "\n\n<% content_for :stylesheets do %>" +
            "\n\t<%= stylesheet_link_tag 'gallery' %>" +
          "\n<% end %>\n\n"
      end
    else
      insert_into_file "app/views/admin/#{@plural_name}/_form.html.erb",
        :after => "<% content_for :javascripts do %>" do
          "\n\t<%= javascript_include_tag 'gallery' %>"
      end
      
      insert_into_file "app/views/admin/#{@plural_name}/_form.html.erb",
        :before => "<% content_for :javascripts do %>" do
          "\n<% content_for :stylesheets do %>" +
            "\n\t<%= stylesheet_link_tag 'gallery' %>" +
          "\n<% end %>\n"
      end
    end
  end
end

#add_tab_to_formObject

Insert into view image gallery tab #########



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/refinerycms-image-gallery.rb', line 159

def add_tab_to_form
  unless File.exists?("#{file_path}/_images_field_#{@chunk}.html.erb")
    page_parts = open("app/views/admin/#{@plural_name}/_form.html.erb").grep(/page_parts/)
    
    if page_parts.empty?
      insert_into_file "app/views/admin/#{@plural_name}/_form.html.erb",
        :before => '<%= render :partial => "/shared/admin/form_actions",' do
          "\n\t<div class='field'>" +
            "\n\t\t<div id='page-tabs' class='clearfix ui-tabs ui-widget ui-widget-content ui-corner-all'>" +
              "\n\t\t\t<ul id='page_parts'>" +
                "\n\t\t\t\t<li class='ui-state-default'>" +
                  "\n\t\t\t\t\t<%= link_to '#{@chunk.camelize} Gallery', '##{@chunk}_gallery' %>" +
                "\n\t\t\t</li>"+
              "\n\t\t\t</ul>" +
              
              "\n\n\t\t\t<div id='page_part_editors'>" +
                "\n\t\t\t\t<div class='page_part' id='#{@chunk}_gallery'>" +
                  "\n\t\t\t\t\t<%= render :partial => 'images', :locals => { :f => f, :chunk_name => '#{@chunk}' } -%>" +
                "\n\t\t\t\t</div>" +
              "\n\t\t\t</div>" +
            "\n\t\t</div>" +
          "\n\t</div>\n"
      end 
    else
      insert_into_file "app/views/admin/#{@plural_name}/_form.html.erb",
        :after => "<ul id='page_parts'>" do
          "\n\t\t<li class='ui-state-default'>" +
            "\n\t\t\t<%= link_to '#{@chunk.camelize} Gallery', '##{@chunk}_gallery' %>" +
          "\n\t\t</li>"
      end 

      insert_into_file "app/views/admin/#{@plural_name}/_form.html.erb",
        :after => "<div id='page_part_editors'>" do
          "\n\t\t<div class='page_part' id='#{@chunk}_gallery'>" +
            "\n\t\t\t<%= render :partial => 'images', :locals => { :f => f, :chunk_name => '#{@chunk}' } -%>" +
          "\n\t\t</div>"
      end
    end
  end
end

#copy_cssObject



279
280
281
# File 'lib/refinerycms-image-gallery.rb', line 279

def copy_css
  template "stylesheets/gallery.css", File.join("public/stylesheets", "gallery.css")  unless @check_migration
end

#copy_javascriptObject



275
276
277
# File 'lib/refinerycms-image-gallery.rb', line 275

def copy_javascript
  template "javascripts/gallery.js", File.join("public/javascripts", "gallery.js") unless @check_migration
end

#create_modelObject

Create relationsheep model #########



111
112
113
114
115
# File 'lib/refinerycms-image-gallery.rb', line 111

def create_model
  unless @check_migration
    template 'models/model.rb', File.join('app/models', "#{@plural_name}_image.rb")
  end
end

#create_view_templatesObject

Create view partial templates for images in gallery #########



248
249
250
251
252
253
254
255
256
257
258
# File 'lib/refinerycms-image-gallery.rb', line 248

def create_view_templates
  unless @check_migration
    template 'views/images.html.erb', File.join(file_path, "_images.html.erb")
    template 'views/image.html.erb', File.join(file_path, "_image.html.erb")
    template 'views/images_field.html.erb', File.join(file_path, "_images_field_#{@chunk}.html.erb")
  else
    unless File.exists?("#{file_path}/_images_field_#{@chunk}.html.erb")
      template 'views/images_field.html.erb', File.join(file_path, "_images_field_#{@chunk}.html.erb")
    end
  end
end

#generate_migrationObject



66
67
68
69
70
71
# File 'lib/refinerycms-image-gallery.rb', line 66

def generate_migration
  unless @check_migration
    migration_template 'migration.rb', "db/migrate/create_#{@plural_name}_images"
    run "rake db:migrate"
  end
end

#insert_chunk_admin_jsObject

Inserto into admin javascript chunk params #########



265
266
267
268
269
270
271
272
273
# File 'lib/refinerycms-image-gallery.rb', line 265

def insert_chunk_admin_js
  js_admin_path = "public/javascripts/refinery/admin.js"
  unless @check_migration
    insert_into_file "#{js_admin_path}","\n, chunk: null", :after => ", callback: null"
    insert_into_file "#{js_admin_path}", ",chunk", :after => ", init: function(callback"  
    insert_into_file "#{js_admin_path}","\n\t\tthis.chunk = chunk;", :after => "this.callback = callback;"
    insert_into_file "#{js_admin_path}", ", this.chunk", :after => "this.callback(img_selected"
  end
end

#insert_image_paramsObject

Insert into insert image template new params #########



238
239
240
241
242
243
244
245
# File 'lib/refinerycms-image-gallery.rb', line 238

def insert_image_params
  unless @check_migration
    insert_into_file "app/views/admin/images/insert.html.erb",
      :after => 'image_dialog.init(<%= @callback.present? ? "self.parent.#{@callback}" : "null" %>' do
        ", '<%= params[:chunk] %>'"
    end
  end
end

#insert_into_modelObject

Insert into models new relations #########



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
# File 'lib/refinerycms-image-gallery.rb', line 118

def insert_into_model
  unless @check_migration
    @columns = []
    @column_name.each do |column|
      @columns << ":" + column.to_s + " => image_data['#{column}']"
    end
  
    # Insert into image model
    insert_into_file "app/models/image.rb", :after => "image_accessor :image\n" do
      "\thas_many :#{@plural_name}_images\n" +
      "\thas_many :#{@plural_name}, :through => :#{@plural_name}_images\n"
    end   
  
    # Insert into object model
    insert_into_file "app/models/#{@name}.rb", :after => "ActiveRecord::Base\n" do
      "\n\thas_many :#{@plural_name}_images\n" +
      "\thas_many :images, :through => :#{@plural_name}_images\n" +
      "\n\tdef images_attributes=(data)
    #{plural_class_name}Image.delete_all(:#{@name}_id => self.id)
    data.each_with_index do | ( k, image_data ), i |
      if image_data['id'].present?
        image_gallery = self.#{@plural_name}_images.new(
                                                        :image_id => image_data['id'].to_i, 
                                                        :position => i, 
                                                        :chunk => image_data['chunk'], 
                                                        #{@columns.join(', ')}
                                                      )
        self.#{@plural_name}_images << image_gallery
      end
      self.touch
    end
  end\n"
    end   
  end
end

#override_class_formObject

Override your class admin/_form item if not exists #########



90
91
92
93
94
# File 'lib/refinerycms-image-gallery.rb', line 90

def override_class_form
  unless File.exists?("app/views/admin/#{@plural_name}/_form.html.erb")
    run "rake refinery:override view=admin/#{@plural_name}/_form"
  end
end

#override_class_modelObject

Override the class model if not exists #########



83
84
85
86
87
# File 'lib/refinerycms-image-gallery.rb', line 83

def override_class_model
  unless File.exists?("app/models/#{@name}.rb")
    run "rake refinery:override model=#{@name}"
  end
end

#override_insert_imageObject

Override inset image view if not exists #########



97
98
99
# File 'lib/refinerycms-image-gallery.rb', line 97

def override_insert_image
  run "rake refinery:override view=admin/images/insert"
end

#override_javascriptsObject

Override refinery admin javascript #########



102
103
104
# File 'lib/refinerycms-image-gallery.rb', line 102

def override_javascripts
  run "rake refinery:override javascript=refinery/admin"
end