Giter Site home page Giter Site logo

nifty-attachments's Introduction

Nifty Attachments

Nifty attachments allow you to attach files/images/documents to Active Record models with ease. Just define which attachments you wish to add and you can easily upload them to your database.

  • All attachment data is stored in your database
  • Attachments are accessed at /attachment/{token}/{filename}. Middleware is provided to provide endpoint.

Example Usage

In order to get started, add the gem to your Gemfile:

gem 'nifty-attachments'

Once included, add the database table which will store your attachments.

$ rails generate nifty:attachments:migration
$ rake db:migrate

You can then define the attachments which you wish to use on any of your models as such:

class Person < ActiveRecord::Base
  attachment :cover_photo
  attachment :profile_picture
end

You can access any of your attachments easily through the methods as shown below.

# Accessing attachments
person = Person.find(person.id)
person.cover_photo            #=> Nifty::Attachments::Attachment
person.cover_photo.path       #=> "/attachment/145d17ed-d5e3-4b55-8c89-ecad9521ad73/snom-mm2.jpg"
person.cover_photo.file_name  #=> "snom-mm2.jpg"
person.cover_photo.digest     #=> "5d41402abc4b2a76b9719d911017c592"

# Pre-loading attachments will only load meta data, the actual content will
# not be loaded.
people = Person.includes(:profile_picture)

You can upload attachments straight from forms into your models by using the _file accessor which is provided.

<% form_for @person, :html => {:multipart => true} do |f| %>
  <%= f.file_field :profile_picture_file %>
  <%= f.file_field :cover_photo_file %>
  <%= f.submit "Upload Attachments" %>
<% end %>

Coming Soon

There are a few extra things which need adding to this library:

  • A test suite
  • Validations

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.