Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create template for controller #3

Open
wbotelhos opened this issue May 9, 2018 · 0 comments
Open

Create template for controller #3

wbotelhos opened this issue May 9, 2018 · 0 comments

Comments

@wbotelhos
Copy link
Owner

wbotelhos commented May 9, 2018

This controller can create assigned URL for JS requests.

# frozen_string_literal: true

class AttachyController < ApplicationController
  skip_authorization_check

  def url
    image = Cloudinary::Utils.cloudinary_url(params[:public_id], image_options)
    link  = Cloudinary::Utils.cloudinary_url(params[:public_id], link_options)

    render json: { image: image, link: link }
  end

  private

  def options
    {
      format:   params[:format],
      secure:   params[:secure],
      sign_url: params[:sign_url]
    }
  end

  def image_options
    {
      crop:    params[:crop],
      height:  params[:height],
      version: params[:version],
      width:   params[:width]
    }.merge options
  end

  def link_options
    {
      crop:    params[:link_crop],
      height:  params[:link_height],
      version: params[:link_version],
      width:   params[:link_width]
    }.merge options
  end
end
# frozen_string_literal: true

require 'common_helper'
require 'support/database_cleaner'
require 'support/factory_bot'
require 'support/shared/logged'

RSpec.describe AttachyController, '#url' do
  let!(:public_id) { 7 }

  let!(:image_parameters) do
    {
      crop:    'fill',
      height:  '1',
      version: '1',
      width:   '1'
    }
  end

  let!(:link_parameters) do
    {
      link_crop:    'crop',
      link_height:  '2',
      link_version: '2',
      link_width:   '2'
    }
  end

  let!(:options) do
    {
      format:   'jpg',
      secure:   'true',
      sign_url: 'true'
    }
  end

  before  do
    allow(Cloudinary::Utils).to receive(:cloudinary_url).with('7',
      crop:    'fill',
      height:  '1',
      version: '1',
      width:   '1',

      format:   'jpg',
      secure:   'true',
      sign_url: 'true'
    ).and_return(:image)

    allow(Cloudinary::Utils).to receive(:cloudinary_url).with('7', {
      crop:    'crop',
      height:  '2',
      version: '2',
      width:   '2',

      format:   'jpg',
      secure:   'true',
      sign_url: 'true'
    }).and_return(:link)
  end

  it 'serializes the image and link urls with given transformations' do
    get :url, params: image_parameters.merge(link_parameters).merge(options).merge(public_id: public_id)

    expect(json_for(response)).to eq(image: 'image', link: 'link')
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant