Skip to content

Instantly share code, notes, and snippets.

@cintrzyk
Created October 28, 2015 17:26
Show Gist options
  • Save cintrzyk/6105317f56cdfd354b54 to your computer and use it in GitHub Desktop.
Save cintrzyk/6105317f56cdfd354b54 to your computer and use it in GitHub Desktop.
test_code_snippet
class Item
class Ng.RailsViews.AdminBlogImagesNewView extends Backbone.View
FILE_TYPES: ['image/png', 'image/jpeg', 'image/gif']
IMAGE_WIDTH = 100
el: 'body'
events:
'dragenter #blog-images .drop': 'dragEnter'
'dragover #blog-images .drop': 'dragOver'
'drop #blog-images .drop': 'drop',
'click #upload': 'clickUpload',
'click #remove-all': 'clickRemoveAll',
'click button.remove': 'clickRemove'
initialize: () ->
@id = 0
@files = {}
@images = {}
@$list = $('#blog-images .list')
@$upload = $('#upload')
dragEnter: (event) ->
event.stopPropagation()
event.preventDefault()
false
dragOver: (event) ->
event.stopPropagation()
event.preventDefault()
false
drop: (event) ->
event.stopPropagation()
event.preventDefault()
@readFiles(event.originalEvent.dataTransfer.files)
clickRemoveAll: (event) ->
@$list.empty()
@files = @images = {}
@disableForm()
clickUpload: (event) ->
count = 0
all = @filesCount()
@disableForm()
@updateProgress(0)
$('.list').children().each (index, element) =>
$name = $(element).find('input[name="name"]')
name = $name.val()
if name == ''
all -= 1
@inputError $name, 'is required'
return
else
@inputClearError $name
id = $(element).data().id
author = $(element).find('input[name="author"]').val()
source = $(element).find('input[name="source"]').val()
formData = @createFormData(name, author, source, @files[id])
@sendXhr formData, (response) =>
if response.status == 'ok'
count += 1
@updateProgress((count / all) * 100)
delete @files[id]
delete @images[id]
$(element).remove()
else
all -= 1
@updateProgress((count / all) * 100)
@inputError($name, response.errors.name)
if count == all
@enableForm()
if all == 0
@updateProgress(0)
@enableForm()
clickRemove: (event) ->
$row = $(event.target).parent().parent()
id = $row.data().id
$row.remove()
delete @files[id]
delete @images[id]
unless @filesCount() > 0
@disableForm()
filesCount: () ->
Object.keys(@files).length
enableForm: () ->
$('#blog-images button').removeClass('disabled')
$('#blog-images input').removeAttr('disabled')
disableForm: () ->
$('#blog-images button,input').addClass('disabled')
$('#blog-images input').attr('disabled', 'disabled')
readFiles: (files) ->
return unless files.length > 0
for f in files
continue if f.type not in @FILE_TYPES
@readFile(f)
if @filesCount() > 0
@enableForm()
readFile: (file) ->
id = @id++
@files[id] = file
reader = new FileReader()
reader.onload = (event) =>
image = @createImage(event.target.result)
@images[id] = image
row = new Ng.RailsViews.AdminBlogImagesRowView(image, id)
@$list.append(row.render())
reader.readAsDataURL(file)
createImage: (src) ->
image = new Image()
image.src = src
image.width = @IMAGE_WIDTH
image
include Mongoid::Document
include Mongoid::Timestamps
> twitter_data = { username: "dev_sufler", bio: "", website:"", profile_picture: "http://images.ak.instagram.com/profiles/anonymousUser.jpg", full_name: "", counts: { media: 1, followed_by: 0, follows: 0 }, id: "1000000001"}
> TwitterUser.create!(name: twitter_data[:username], social_id: twitter_data[:id], custom_fields: twitter_data)
> TwitterUser.find_by(social_id: '1000000001')
TwitterUser Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."type" IN ('TwitterUser') AND "users"."social_id" = '1000000001' LIMIT 1
=> #<TwitterUser id: 8, name: "dev_sufler", type: "TwitterUser", custom_fields: {"username"=>"dev_sufler", "bio"=>"", "website"=>"", "profile_picture"=>"http://images.ak.instagram.com/profiles/anonymousUser.jpg", "full_name"=>"", "counts"=>{"media"=>1, "followed_by"=>0, "follows"=>0}, "id"=>"1000000001"}, created_at: "2014-05-11 09:15:37", updated_at: "2014-05-11 09:15:37", social_id: "1000000001">
view rawtwitter_console.rb hosted with ❤ by GitHub
class Item
include Mongoid::Document
include Mongoid::Timestamps
> twitter_data = { username: "dev_sufler", bio: "", website:"", profile_picture: "http://images.ak.instagram.com/profiles/anonymousUser.jpg", full_name: "", counts: { media: 1, followed_by: 0, follows: 0 }, id: "1000000001"}
> TwitterUser.create!(name: twitter_data[:username], social_id: twitter_data[:id], custom_fields: twitter_data)
> TwitterUser.find_by(social_id: '1000000001')
TwitterUser Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."type" IN ('TwitterUser') AND "users"."social_id" = '1000000001' LIMIT 1
=> #<TwitterUser id: 8, name: "dev_sufler", type: "TwitterUser", custom_fields: {"username"=>"dev_sufler", "bio"=>"", "website"=>"", "profile_picture"=>"http://images.ak.instagram.com/profiles/anonymousUser.jpg", "full_name"=>"", "counts"=>{"media"=>1, "followed_by"=>0, "follows"=>0}, "id"=>"1000000001"}, created_at: "2014-05-11 09:15:37", updated_at: "2014-05-11 09:15:37", social_id: "1000000001">
view rawtwitter_console.rb hosted with ❤ by GitHub
class Item
include Mongoid::Document
include Mongoid::Timestamps
> twitter_data = { username: "dev_sufler", bio: "", website:"", profile_picture: "http://images.ak.instagram.com/profiles/anonymousUser.jpg", full_name: "", counts: { media: 1, followed_by: 0, follows: 0 }, id: "1000000001"}
> TwitterUser.create!(name: twitter_data[:username], social_id: twitter_data[:id], custom_fields: twitter_data)
> TwitterUser.find_by(social_id: '1000000001')
TwitterUser Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."type" IN ('TwitterUser') AND "users"."social_id" = '1000000001' LIMIT 1
=> #<TwitterUser id: 8, name: "dev_sufler", type: "TwitterUser", custom_fields: {"username"=>"dev_sufler", "bio"=>"", "website"=>"", "profile_picture"=>"http://images.ak.instagram.com/profiles/anonymousUser.jpg", "full_name"=>"", "counts"=>{"media"=>1, "followed_by"=>0, "follows"=>0}, "id"=>"1000000001"}, created_at: "2014-05-11 09:15:37", updated_at: "2014-05-11 09:15:37", social_id: "1000000001">
view rawtwitter_console.rb hosted with ❤ by GitHub
field :name, type: String
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment