All Projects → rootstrap → active-storage-base64

rootstrap / active-storage-base64

Licence: MIT license
Base64 support for ActiveStorage

Programming Languages

ruby
36898 projects - #4 most used programming language
HTML
75241 projects
javascript
184084 projects - #8 most used programming language
CSS
56736 projects

Projects that are alternatives of or similar to active-storage-base64

souls
SOULs 🔥 Build Serverless Apps faster like Rails. Powered by Ruby GraphQL, RBS/Steep, Active Record, RSpec, RuboCop, and Google Cloud.
Stars: ✭ 327 (+170.25%)
Mutual labels:  ruby-on-rails
mobility-actiontext
Translate Rails Action Text rich text with Mobility.
Stars: ✭ 27 (-77.69%)
Mutual labels:  ruby-on-rails
from-fat-controllers-to-use-cases
Rails (API) app that shows different kinds of architecture (one per commit), and in the last one, how to use the Micro::Case gem to handle the application business logic.
Stars: ✭ 74 (-38.84%)
Mutual labels:  ruby-on-rails
kickstart
Ruby on Rails application templates
Stars: ✭ 61 (-49.59%)
Mutual labels:  ruby-on-rails
stimulus reflex
Build reactive applications with the Rails tooling you already know and love.
Stars: ✭ 2,001 (+1553.72%)
Mutual labels:  ruby-on-rails
Marketplace-App
Find Spelling errors in files within PRs
Stars: ✭ 47 (-61.16%)
Mutual labels:  ruby-on-rails
invoiced
Invoiced Demo app for Rails API and React Foundation
Stars: ✭ 33 (-72.73%)
Mutual labels:  rails-api
SpeedUpAmerica
Crowd-sourced internet speed tests using M-Lab data and user tests on a website, with charts, maps, and raw data downloads.
Stars: ✭ 24 (-80.17%)
Mutual labels:  ruby-on-rails
modular routes
Dedicated controllers for each of your Rails route actions.
Stars: ✭ 45 (-62.81%)
Mutual labels:  ruby-on-rails
doorkeeper-sequel
Doorkeeper Sequel ORM
Stars: ✭ 12 (-90.08%)
Mutual labels:  ruby-on-rails
velum
Dashboard for CaaS Platform clusters (v1, v2 and v3)
Stars: ✭ 55 (-54.55%)
Mutual labels:  ruby-on-rails
awesome-rails-security
A curated list of security resources for a Ruby on Rails application
Stars: ✭ 36 (-70.25%)
Mutual labels:  ruby-on-rails
agenda-saude
Sistema de agendamento de saúde, em uso para gerir filas de vacinação do COVID-19 e H1N1.
Stars: ✭ 119 (-1.65%)
Mutual labels:  ruby-on-rails
human-in-the-loop-machine-learning-tool-tornado
Tornado is a human-in-the-loop machine learning framework that helps you exploit your unlabelled data to train models through a simple and easy to use web interface.
Stars: ✭ 37 (-69.42%)
Mutual labels:  ruby-on-rails
IndieNoMo
A full-stack web-app inspired by crowdfunding platform IndieGoGo.
Stars: ✭ 36 (-70.25%)
Mutual labels:  ruby-on-rails
darrrr
An SDK for the delegated recovery specfication
Stars: ✭ 43 (-64.46%)
Mutual labels:  ruby-on-rails
sidekiq-sequence
Sequential Sidekiq jobs for Rails
Stars: ✭ 38 (-68.6%)
Mutual labels:  ruby-on-rails
nexmo-oas-renderer
Render your API references, Nexmo-style!
Stars: ✭ 40 (-66.94%)
Mutual labels:  ruby-on-rails
rails cursor pagination
Add cursor pagination to your ActiveRecord backed application
Stars: ✭ 21 (-82.64%)
Mutual labels:  ruby-on-rails
china regions
Ruby Library for China Regions
Stars: ✭ 23 (-80.99%)
Mutual labels:  ruby-on-rails

CI Maintainability Test Coverage

ActiveStorageBase64

Adds support for base64 attachments to ActiveStorage.

Installation

In order to get the gem working on your project you just need to add the gem to your project like this:

gem 'active_storage_base64'

Compatibility

Rails Version ActiveStorageBase64 Version
7.0.x 2.0.x
6.1.x 1.2.x
6.0.x 1.1.x
5.2.x 0.1.x

Prerequisites

The only prerequisite for using this gem is having ActiveStorage properly set up in your project. For more information on how to do this, check Active Storage Overview.

Usage

In order to use the gem's functionality, you need to include the ActiveStorageSupport::SupportForBase64 module in your ActiveRecord models. For example:

class User < ActiveRecord::Base
  include ActiveStorageSupport::SupportForBase64
end

Note: We highly recommend using an alternative class that inherits from ActiveRecord::Base and includes the module so instead of including the module for each of your classes, you make them inherit from this new class, check below:

class ApplicationRecord < ActiveRecord::Base
  include ActiveStorageSupport::SupportForBase64
end

class User < ApplicationRecord
  has_one_base64_attached :avatar
end

After you have the module included in your class you'll be able to use the following two helper methods to work with base64 files: When you need a single image attached:

has_one_base64_attached

and when you need multiple files attached:

has_many_base64_attached

These helpers will work just like the has_one_attached and has_many_attached helper methods from ActiveStorage.

A working example for this, assuming we have a model User with an avatar attached would be:

class User < ActiveRecord::Base
  include ActiveStorageSupport::SupportForBase64

  has_one_base64_attached :avatar
end

on your controller you could do any of the following:

class UsersController < ApplicationController
  def create
    user = User.create(user_params)
  end

  private

  def user_params
    params.require(:user).permit(avatar: :data, :username, :email)
  end
end
class UsersController < ApplicationController
  def create
    user = User.create(user_params)
    user.avatar.attach(data: params[:avatar]) # params[:avatar] => 'data:image/png;base64,[base64 data]'
  end

  private

  def user_params
    params.require(:user).permit(:username, :email)
  end
end
class UsersController < ApplicationController
  def create
    user = User.create(user_params)
    user.avatar.attach(avatar_params) # avatar_params => { data: 'data:image/png;base64,[base64 data]' }
  end

  private

  def user_params
    params.require(:user).permit(:username, :email)
  end

  def avatar_params
    params.require(:avatar).permit(:data)
  end
end
class UsersController < ApplicationController
  def create
    user = User.create(user_params)
    user.avatar = { data: params[:avatar] } # params[:avatar] => 'data:image/png;base64,[base64 data]'
    user.save
  end

  private

  def user_params
    params.require(:user).permit(:username, :email)
  end
end

Specifying a filename or content type

If you are willing to add a specific filename to your attachment, or send in a specific content type for your file, you can use data: to attach the base64 data and specify your filename:, content_type: and/or identify: hash keys. Check the following example:

class UsersController < ApplicationController
  def create
    user = User.create(user_params)
    user.avatar.attach(data: params[:avatar], filename: 'your_filename', content_type: 'content/type', identify: 'false') # params[:avatar] => 'data:image/png;base64,[base64 data]'
  end

  private

  def user_params
    params.require(:user).permit(:username, :email)
  end
end

Or, in case you want to have the avatar attached as soon as the user is created you can do:

class UsersController < ApplicationController
  def create
    user = User.create(user_params)
  end

  private

  def user_params
    params.require(:user).permit(:username, :email, avatar: [:data,
                                                             :filename,
                                                             :content_type,
                                                             :identify])
  end
end

Data Format

To attach base64 data it is required to come in the form of Data URIs . For example:

data:image/png;base64,[base64 data]

Contributing

Please read our CONTRIBUTING and our CODE_OF_CONDUCT files for details on our code of conduct, and the process for submitting pull requests to us.

Author

Ricardo Cortio

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgments

Special thanks to the people who helped with guidance and ensuring code quality in this project: Santiago Bartesaghi, Santiago Vidal and Matias Mansilla.

Credits

Active Storage Base64 is maintained by Rootstrap with the help of our contributors.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].