Rails paperclip default image only under certain conditions










0















I've got a Lead model which is splited by lead_status param on products and offers. Only leads with product status should contain images and offers should not. I've migrated attached product_image table to schema and tried to set a default image only for products. Like this:



class Lead < ApplicationRecord


has_attached_file :product_image, styles: small: "150x150>", default: "350x350"
validates_attachment_content_type :product_image, content_type: /Aimage/.*z/

before_save :product_image_default_url

def product_image_default_url
if self.lead_status == "product" && self.product_image.url.nil?
self.product_image.url = "/images/:style/default_user_avatar.png"
end
end


  1. Every time when I save a new lead without uploaded image I get "/product_images/original/missing.png" as a default url. No matter which status it has.

  2. Model doesn't recognize new leads by it's status

How can I change that? Force my Lead model to save a default image url according to a "product" status and ignore all those with "offer" status?



My rails version is 5.2.1 and paperclip 6.0.0










share|improve this question






















  • Not sure if it still works, but have a look at this gist.github.com/nazarhussain/1040091/…

    – Deepak Mahakale
    Nov 13 '18 at 5:01















0















I've got a Lead model which is splited by lead_status param on products and offers. Only leads with product status should contain images and offers should not. I've migrated attached product_image table to schema and tried to set a default image only for products. Like this:



class Lead < ApplicationRecord


has_attached_file :product_image, styles: small: "150x150>", default: "350x350"
validates_attachment_content_type :product_image, content_type: /Aimage/.*z/

before_save :product_image_default_url

def product_image_default_url
if self.lead_status == "product" && self.product_image.url.nil?
self.product_image.url = "/images/:style/default_user_avatar.png"
end
end


  1. Every time when I save a new lead without uploaded image I get "/product_images/original/missing.png" as a default url. No matter which status it has.

  2. Model doesn't recognize new leads by it's status

How can I change that? Force my Lead model to save a default image url according to a "product" status and ignore all those with "offer" status?



My rails version is 5.2.1 and paperclip 6.0.0










share|improve this question






















  • Not sure if it still works, but have a look at this gist.github.com/nazarhussain/1040091/…

    – Deepak Mahakale
    Nov 13 '18 at 5:01













0












0








0








I've got a Lead model which is splited by lead_status param on products and offers. Only leads with product status should contain images and offers should not. I've migrated attached product_image table to schema and tried to set a default image only for products. Like this:



class Lead < ApplicationRecord


has_attached_file :product_image, styles: small: "150x150>", default: "350x350"
validates_attachment_content_type :product_image, content_type: /Aimage/.*z/

before_save :product_image_default_url

def product_image_default_url
if self.lead_status == "product" && self.product_image.url.nil?
self.product_image.url = "/images/:style/default_user_avatar.png"
end
end


  1. Every time when I save a new lead without uploaded image I get "/product_images/original/missing.png" as a default url. No matter which status it has.

  2. Model doesn't recognize new leads by it's status

How can I change that? Force my Lead model to save a default image url according to a "product" status and ignore all those with "offer" status?



My rails version is 5.2.1 and paperclip 6.0.0










share|improve this question














I've got a Lead model which is splited by lead_status param on products and offers. Only leads with product status should contain images and offers should not. I've migrated attached product_image table to schema and tried to set a default image only for products. Like this:



class Lead < ApplicationRecord


has_attached_file :product_image, styles: small: "150x150>", default: "350x350"
validates_attachment_content_type :product_image, content_type: /Aimage/.*z/

before_save :product_image_default_url

def product_image_default_url
if self.lead_status == "product" && self.product_image.url.nil?
self.product_image.url = "/images/:style/default_user_avatar.png"
end
end


  1. Every time when I save a new lead without uploaded image I get "/product_images/original/missing.png" as a default url. No matter which status it has.

  2. Model doesn't recognize new leads by it's status

How can I change that? Force my Lead model to save a default image url according to a "product" status and ignore all those with "offer" status?



My rails version is 5.2.1 and paperclip 6.0.0







ruby-on-rails ruby paperclip






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 4:53









andrzej541andrzej541

889




889












  • Not sure if it still works, but have a look at this gist.github.com/nazarhussain/1040091/…

    – Deepak Mahakale
    Nov 13 '18 at 5:01

















  • Not sure if it still works, but have a look at this gist.github.com/nazarhussain/1040091/…

    – Deepak Mahakale
    Nov 13 '18 at 5:01
















Not sure if it still works, but have a look at this gist.github.com/nazarhussain/1040091/…

– Deepak Mahakale
Nov 13 '18 at 5:01





Not sure if it still works, but have a look at this gist.github.com/nazarhussain/1040091/…

– Deepak Mahakale
Nov 13 '18 at 5:01












1 Answer
1






active

oldest

votes


















1














Try with following,



has_attached_file 
:product_image,
styles: small: "150x150>", default: "350x350",
default_url: ":style/default_user_avatar.png"

# app/assets/images/medium/default_user_avatar.png
# app/assets/images/thumbs/default_user_avatar.png


Existing method is,



def default_url
if @attachment_options[:default_url].respond_to?(:call)
@attachment_options[:default_url].call(@attachment)
elsif @attachment_options[:default_url].is_a?(Symbol)
@attachment.instance.send(@attachment_options[:default_url])
else
@attachment_options[:default_url]
end
end


In initializer, Provide monkey patch for following,



require 'uri'
require 'active_support/core_ext/module/delegation'

module Paperclip
class UrlGenerator

def default_url
if @attachment.instance.lead_status == 'product'
default_url = attachment_options[:default_url]
else
default_url = # provide another missing default_url
end
if default_url.respond_to?(:call)
default_url.call(@attachment)
elsif default_url.is_a?(Symbol)
@attachment.instance.send(default_url)
else
default_url
end
end

end
end


Update as per cases






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274036%2frails-paperclip-default-image-only-under-certain-conditions%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Try with following,



    has_attached_file 
    :product_image,
    styles: small: "150x150>", default: "350x350",
    default_url: ":style/default_user_avatar.png"

    # app/assets/images/medium/default_user_avatar.png
    # app/assets/images/thumbs/default_user_avatar.png


    Existing method is,



    def default_url
    if @attachment_options[:default_url].respond_to?(:call)
    @attachment_options[:default_url].call(@attachment)
    elsif @attachment_options[:default_url].is_a?(Symbol)
    @attachment.instance.send(@attachment_options[:default_url])
    else
    @attachment_options[:default_url]
    end
    end


    In initializer, Provide monkey patch for following,



    require 'uri'
    require 'active_support/core_ext/module/delegation'

    module Paperclip
    class UrlGenerator

    def default_url
    if @attachment.instance.lead_status == 'product'
    default_url = attachment_options[:default_url]
    else
    default_url = # provide another missing default_url
    end
    if default_url.respond_to?(:call)
    default_url.call(@attachment)
    elsif default_url.is_a?(Symbol)
    @attachment.instance.send(default_url)
    else
    default_url
    end
    end

    end
    end


    Update as per cases






    share|improve this answer



























      1














      Try with following,



      has_attached_file 
      :product_image,
      styles: small: "150x150>", default: "350x350",
      default_url: ":style/default_user_avatar.png"

      # app/assets/images/medium/default_user_avatar.png
      # app/assets/images/thumbs/default_user_avatar.png


      Existing method is,



      def default_url
      if @attachment_options[:default_url].respond_to?(:call)
      @attachment_options[:default_url].call(@attachment)
      elsif @attachment_options[:default_url].is_a?(Symbol)
      @attachment.instance.send(@attachment_options[:default_url])
      else
      @attachment_options[:default_url]
      end
      end


      In initializer, Provide monkey patch for following,



      require 'uri'
      require 'active_support/core_ext/module/delegation'

      module Paperclip
      class UrlGenerator

      def default_url
      if @attachment.instance.lead_status == 'product'
      default_url = attachment_options[:default_url]
      else
      default_url = # provide another missing default_url
      end
      if default_url.respond_to?(:call)
      default_url.call(@attachment)
      elsif default_url.is_a?(Symbol)
      @attachment.instance.send(default_url)
      else
      default_url
      end
      end

      end
      end


      Update as per cases






      share|improve this answer

























        1












        1








        1







        Try with following,



        has_attached_file 
        :product_image,
        styles: small: "150x150>", default: "350x350",
        default_url: ":style/default_user_avatar.png"

        # app/assets/images/medium/default_user_avatar.png
        # app/assets/images/thumbs/default_user_avatar.png


        Existing method is,



        def default_url
        if @attachment_options[:default_url].respond_to?(:call)
        @attachment_options[:default_url].call(@attachment)
        elsif @attachment_options[:default_url].is_a?(Symbol)
        @attachment.instance.send(@attachment_options[:default_url])
        else
        @attachment_options[:default_url]
        end
        end


        In initializer, Provide monkey patch for following,



        require 'uri'
        require 'active_support/core_ext/module/delegation'

        module Paperclip
        class UrlGenerator

        def default_url
        if @attachment.instance.lead_status == 'product'
        default_url = attachment_options[:default_url]
        else
        default_url = # provide another missing default_url
        end
        if default_url.respond_to?(:call)
        default_url.call(@attachment)
        elsif default_url.is_a?(Symbol)
        @attachment.instance.send(default_url)
        else
        default_url
        end
        end

        end
        end


        Update as per cases






        share|improve this answer













        Try with following,



        has_attached_file 
        :product_image,
        styles: small: "150x150>", default: "350x350",
        default_url: ":style/default_user_avatar.png"

        # app/assets/images/medium/default_user_avatar.png
        # app/assets/images/thumbs/default_user_avatar.png


        Existing method is,



        def default_url
        if @attachment_options[:default_url].respond_to?(:call)
        @attachment_options[:default_url].call(@attachment)
        elsif @attachment_options[:default_url].is_a?(Symbol)
        @attachment.instance.send(@attachment_options[:default_url])
        else
        @attachment_options[:default_url]
        end
        end


        In initializer, Provide monkey patch for following,



        require 'uri'
        require 'active_support/core_ext/module/delegation'

        module Paperclip
        class UrlGenerator

        def default_url
        if @attachment.instance.lead_status == 'product'
        default_url = attachment_options[:default_url]
        else
        default_url = # provide another missing default_url
        end
        if default_url.respond_to?(:call)
        default_url.call(@attachment)
        elsif default_url.is_a?(Symbol)
        @attachment.instance.send(default_url)
        else
        default_url
        end
        end

        end
        end


        Update as per cases







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 5:39









        rayray

        1,640319




        1,640319



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274036%2frails-paperclip-default-image-only-under-certain-conditions%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto