Friday, 14 January 2022

Multiple file upload by carrierwave upload any media one's only one column

 Note Add new column `photo_or_video` in your MODEL


In Controller:

Check type: content_type(image or video)

file = img.original_filename

attachment_type = file.split(".").last

video_or_image = attachment_type.downcase

------------------------------------------

 if video_or_image == "png" || video_or_image == "PNG" || video_or_image == "jpg" || video_or_image == "JPG" || video_or_image == "jpeg" || video_or_image == "JPEG" || video_or_image == "gif" || video_or_image == "GIF"

                video_or_image = "image"

              elsif video_or_image == "mp4" || video_or_image == "MP4" || video_or_image == "ogv" || video_or_image == "OGV" || video_or_image == "ogg" || video_or_image == "OGG" || video_or_image == "mov" || video_or_image == "MOV"

                video_or_image = "video"

              end

-------------------------------------------

And:  User.create(attachment: img, photo_or_video: video_or_image)


==========================================================

Multiple file uploads

Add it to your Gemfile:

      gem 'mini_magick'
      gem 'carrierwave'
      gem 'carrierwave-video-thumbnailer'
Than
      bundle

Install packages: sudo apt install ffmpegthumbnailer

Start off by generating an uploader:

app/uploaders/pictures_uploader.rb


      rails generate uploader pictures

Check out this file for some hints on how you can customize your uploader. It should look something like this:


class PicturesUploader < CarrierWave::Uploader::Base
      storage :file
end

If we want to upload video or image then we will have to use :-

class PicturesUploader < CarrierWave::Uploader::Base
      include CarrierWave::MiniMagick
      include CarrierWave::Video
      include CarrierWave::Video::Thumbnailer

      storage :file
   
     def store_dir
         "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
     end

     version :thumb do
          process thumbnail: [{format: 'jpg', quality: 8, size: 360, logger: Rails.logger}]
          def full_filename for_file
              jpg_name for_file, version_name
          end
     end

     version :medium do
           process thumbnail: [{format: 'jpg', quality: 85, size: 300, logger: Rails.logger}]
          def full_filename for_file
              jpg_name for_file, version_name
          end
     end

    # Create different versions of your uploaded files:
    version :thumb,:if => :is_image? do
          process :resize_to_fit => [740, 300]
    end

    def jpg_name for_file, version_name
         %Q{#{version_name}_#{for_file.chomp(File.extname(for_file))}.jpg}
    end

    protected
    def is_image?(resource_file)
      content_type = resource_file.content_type
      extentions = %w(image/jpeg image/png image/jpg image/gif)
      extentions.include? content_type
    end
    def video?(resource_file)
      content_type = resource_file.content_type
      extentions = %w(video/mp4 video/ogv video/ogg video/mov video/quicktime audio/ogg)
      extentions.include? content_type
    end
end

Add a string column to the model you want to mount the uploader by creating a migration:

     rails g migration add_vimeo_file_to_users attachment:string
     rake db:migrate

Open your model file and mount the uploader:

class User < ActiveRecord::Base
     mount_uploader :attachment:, VimeoUploader
end

===================================================


https://railsblogs.rohityadav.in/2017/11/uploading-any-media-file-with-carrier.html


Tuesday, 11 January 2022

Completed conditional versioning in carrierwave based on extension(image or video) with thumbinal

Add gem: 

gem 'mini_magick'

-----------------------------------------------------------------------

Added on uploader file: 


include CarrierWave::MiniMagick


version :thumb, :if => :is_image?  do

    process resize_to_fit: [450, 400]

  end


protected

  def is_image?(resource_file)

    content_type = resource_file.content_type

    extentions = %w(image/jpeg image/png image/jpg image/gif)

    extentions.include? content_type

  end 

Friday, 12 November 2021

Active admin textbox/search functionality for selecting

 Add gem: 

gem 'activeadmin-searchable_select'

Import stylesheets and require javascripts:

// active_admin.scss
@import "active_admin/mixins";
@import "active_admin/base";

// active_admin.js
//= require active_admin/base
Add admin site in form field
f.input :account_id, as: :searchable_select, collection: AccountBlock::Account.all.map { |s| [s.profile.try(:name), s.id]}

Add ckeditor in active admin site

 


Gemfile: gem 'ckeditor'

active_admin.rb:  config.register_javascript "https://cdn.ckeditor.com/4.16.1/standard/ckeditor.js"

 admin_input:  f.input :contract, :input_html => { :class => "ckeditor" , :height => 400}

Friday, 1 October 2021

change Data base mysql to pg

 1. Create same database(database_name-thera_dev) both rails application in mysql and pg

2. Import sql file in mysql rails application.

3. then run this command for pg rails application .

"

pgloader mysql://root:root@127.0.0.1/DATABASE_NAME_SAME postgresql://root:root@127.0.0.1/DATABASE_NAME_SAME

"


4. then Export pg dupm file---this command run pg application

pg_dump -U root DATABASE_NAME_SAME > sql_file_name.sql


Thursday, 16 September 2021

Add Balance on stripe account

 token = Stripe::Token.create({

  card: {

    number: '4000000000000077',

    exp_month: 7,

    exp_year: 2022,

    cvc: '314',

  },

})


Stripe::Charge.create({

    'currency' => 'USD',

    'amount'   => 20000000,

    'source'   => token['id']

});

Wednesday, 8 September 2021

One form multiple time create

 <button type='button' onclick="addMore()">Add More (+)</button>

<%= form_with(model: article, id: "new_user") do |form| %>

  <% if article.errors.any? %>

    <div id="error_explanation">

      <h2><%= pluralize(article.errors.count, "error") %> prohibited this article from being saved:</h2>


      <ul>

        <% article.errors.each do |error| %>

          <li><%= error.full_message %></li>

        <% end %>

      </ul>

    </div>

  <% end %>


  <div class="field">

    <%= form.label :title %>

    <%= text_field_tag :title %>

  </div>

  <div id="dynamic-inputs"></div>

  <div class="actions">

    <%#= form.submit %>

  </div>

<% end %>

<button id='sendRequest' onclick="submiForm()">submit</button>


<script>

  function addMore(){

    $("#dynamic-inputs").append($("#new_user").html())

  }


  function submiForm() {

    let data = []

    $("[name='title']").each((i, item)=>

    {

    // debugger

      data.push({'title': item.value})

    })

    console.log({'quote_amounts': data})

    $.ajax({

        url: '/articles',

        data: {'quote_amounts': data},

        type: 'POST',

        success: function(data){

            // handle your success here

            console.log(data);

        }

    });

  }

</script>



For articles controller:

def create

    params[:quote_amounts].values.each_with_index do |ar,index|

      @article = Article.create(title: ar[:title], user_id: User.last.id)

    end

    # @article = Article.new(article_params)

    # @article.user_id = current_user.id

    # respond_to do |format|

    #   if @article.save

    #     # UserChannel.create_article(@article, current_user)

    #     format.html { redirect_to @article, notice: "Article was successfully created." }

    #     format.json { render :show, status: :created, location: @article }

    #   else

    #     format.html { render :new, status: :unprocessable_entity }

    #     format.json { render json: @article.errors, status: :unprocessable_entity }

    #   end

    # end

  end