Last active
March 28, 2017 11:07
-
-
Save michon/f36c1663358ac7a899c004ac99ffa701 to your computer and use it in GitHub Desktop.
app/models/imageninicio.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I, [2017-03-28T12:58:20.078441 #3189] INFO -- : Completed 500 Internal Server Error in 86ms (ActiveRecord: 27.9ms) | |
F, [2017-03-28T12:58:20.080385 #3189] FATAL -- : | |
Sprockets::FileOutsidePaths (/var/www/clients/client1/web52/tmp/generate_css/_comImageninicio.scss is no longer under a load path: /var/www/clients/client1/web52/app/assets/fonts, /var/www/clients/client1/web52/app/assets/images, /var/www/clients/client1/web52/app/assets/javascripts, /var/www/clients/client1/web52/app/assets/stylesheets, /var/www/clients/client1/web52/vendor/assets/javascripts, /var/www/clients/client1/web52/vendor/assets/stylesheets, /var/lib/gems/2.1.0/gems/ahoy_matey-1.2.1/vendor/assets/javascripts, /var/lib/gems/2.1.0/gems/bxslider-rails-4.2.5.1/vendor/assets/images, /var/lib/gems/2.1.0/gems/bxslider-rails-4.2.5.1/vendor/assets/javascripts, /var/lib/gems/2.1.0/gems/bxslider-rails-4.2.5.1/vendor/assets/stylesheets, /var/lib/gems/2.1.0/gems/chartkick-2.0.0/app/assets/javascripts, /var/lib/gems/2.1.0/gems/chartkick-2.0.0/vendor/assets/javascripts, /var/lib/gems/2.1.0/gems/lightbox2-rails-2.7.1/vendor/assets/images, /var/lib/gems/2.1.0/gems/lightbox2-rails-2.7.1/vendor/assets/javascripts, /var/lib/gems/2.1.0/gems/lightbox2-rails-2.7.1/vendor/assets/stylesheets, /var/lib/gems/2.1.0/gems/leaflet-rails-0.7.7/vendor/assets/images, /var/lib/gems/2.1.0/gems/leaflet-rails-0.7.7/vendor/assets/javascripts, /var/lib/gems/2.1.0/gems/leaflet-rails-0.7.7/vendor/assets/stylesheets, /var/lib/gems/2.1.0/gems/activeadmin-1.0.0.pre1/app/assets/images, /var/lib/gems/2.1.0/gems/activeadmin-1.0.0.pre1/app/assets/javascripts, /var/lib/gems/2.1.0/gems/activeadmin-1.0.0.pre1/app/assets/stylesheets, /var/lib/gems/2.1.0/gems/jquery-ui-rails-5.0.5/app/assets/images, /var/lib/gems/2.1.0/gems/jquery-ui-rails-5.0.5/app/assets/javascripts, /var/lib/gems/2.1.0/gems/jquery-ui-rails-5.0.5/app/assets/stylesheets, /var/lib/gems/2.1.0/gems/formtastic-3.1.3/app/assets/stylesheets, /var/lib/gems/2.1.0/gems/ckeditor-4.1.4/app/assets/javascripts, /var/lib/gems/2.1.0/gems/ckeditor-4.1.4/app/assets/stylesheets, /var/lib/gems/2.1.0/gems/ckeditor-4.1.4/vendor/assets/javascripts, /var/lib/gems/2.1.0/gems/turbolinks-2.5.3/lib/assets/javascripts, /var/lib/gems/2.1.0/gems/jquery-rails-4.0.5/vendor/assets/javascripts, /var/lib/gems/2.1.0/gems/coffee-rails-4.1.0/lib/assets/javascripts, /var/lib/gems/2.1.0/gems/bourbon-3.2.4/app/assets/stylesheets): | |
app/models/imageninicio.rb:119:in `asset_source' | |
app/models/imageninicio.rb:99:in `generate_css' | |
app/models/imageninicio.rb:69:in `actualizar_css' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding : utf-8 -*- | |
class Imageninicio < ActiveRecord::Base | |
after_save :actualizar_css | |
translates :titulo, :texto | |
# En la plantilla | |
# has_attached_file :file, :styles => {:muygrande => '1500x1000', :grande => '800x800', :medium => '300x300>', :thumb => '100x100>', :icon => '64x64>' }, :default_url => '/images/:style/missing.png' | |
# Nuevo modelo | |
has_attached_file :imagen, :styles => { | |
# Relación de aspecto 16:9. Descomentar las que se vayan a utilizar en | |
# <picture> | |
:lands_1x => "320x180", | |
:lands_2x => "640x360", | |
:lands_3x => "960x540", | |
:lands_4x => "1280x720", | |
:lands_5x => "1600x900", | |
:lands_6x => "1920x1080", | |
:lands_7x => "2240x1260", | |
:lands_8x => "2560x1440", | |
# :port_1x => "360x640", | |
# :port_2x => "720x1280", | |
# :port_3x=> "1080x1920", | |
# :port_2x_2x => "1440x2560", | |
# :port_2x_3x => "2160x3840", | |
#port_3x_2x => Igual que 2x_3x | |
# :port_3x_3x => "3240x5760", | |
:icon_lands => "128x72", | |
:icon_port => "72x128", | |
# :tablet_lands => "800x480", | |
# :tablet_port => "480x800", | |
# Relación de aspecto 4:3. Descomentar las que se vayan a utilizar en | |
# <picture> | |
# :ipad_lands_1x => "640x480", | |
# :ipad_lands_2x => "1280x960", | |
# :ipad_lands_3x => "1400x1050", | |
# :ipad_port_1x => "480x640", | |
# :ipad_port_2x => "960x1280", | |
# :ipad_port_3x => "1050x1440", | |
:facebook => "1200x830", | |
# Ratio cuadrado | |
:icon => "72x72", | |
:thumb => "100x100>" }, | |
convert_options: {all: "-auto-orient"}, | |
:url => "/imagenesinicio/:style/:id/:basename.:extension", | |
:path => "public/imagenesinicio/:style/:id/:basename.:extension" | |
validates_attachment_content_type :imagen, :content_type => /\Aimage\/.*\Z/ | |
#devuelve las dimensiones con las que una imagen se ha guardado en el disco duro | |
# Ya incluye el método file | |
def dimensiones(estilo = :original) | |
Paperclip::Geometry.from_file(self.imagen.path(estilo)) | |
end | |
def actualizar_css | |
@nombreFichero = '_comImageninicio' | |
@ficheroCss = File.new(scss_file_path, 'w') | |
@body = ERB.new(File.read(template_file_path)).result(binding) | |
@env = Rails.application.assets | |
create_scss | |
begin | |
@ficheroCss.write generate_css | |
@ficheroCss.sync = true | |
@ficheroCss.flush | |
ensure | |
@ficheroCss.close | |
File.delete(@ficheroCss) | |
end | |
end | |
def scss_file_path | |
@scss_file_path ||= File.join(scss_tmpfile_path, "#{@nombreFichero}.scss") | |
end | |
def scss_tmpfile_path | |
@scss_file_path ||= File.join(Rails.root, 'tmp', 'generate_css') | |
unless File.exists?(@scss_file_path) | |
FileUtils.mkdir_p(@scss_file_path) | |
end | |
@scss_file_path | |
end | |
def template_file_path | |
@template_file_path ||= File.join(Rails.root, 'app', 'assets', 'stylesheets', 'com', '_comImageninicio.css.scss.erb') | |
end | |
def create_scss | |
File.open(scss_file_path, 'w') {|f| f.write(@body)} | |
end | |
def generate_css | |
Sass::Engine.new(asset_source, { | |
syntax: :scss, | |
cache: false, | |
read_cache: false, | |
style: :compressed | |
}).render | |
end | |
def asset_source | |
#config = Rails.application.config | |
#config.assets.prefix = 'tmp/generate_css' | |
#config.assets.digest = true | |
#target = File.join(Rails.public_path, config.assets.prefix) | |
# @env.append_path target | |
#@env.append_path 'tmp/generate_css' | |
if @env.find_asset(@nombreFichero) | |
@env.find_asset(@nombreFichero).source | |
else | |
uri = Sprockets::URIUtils.build_asset_uri(@scss_file_path, type: "text/css") | |
asset = Sprockets::UnloadedAsset.new(uri,@env) | |
@env.load(asset.uri).source | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment