Skip to content

Commit

Permalink
Merge pull request codeforamerica#43 from fullstacklabs/CS-826
Browse files Browse the repository at this point in the history
CS-826 Ohana support of image loader
  • Loading branch information
callitafeature authored May 2, 2019
2 parents cf27ba8 + 06cceaf commit 03183e1
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 18 deletions.
29 changes: 14 additions & 15 deletions app/controllers/api/v1/blog_posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class BlogPostsController < Api::V1::BaseController
include ErrorSerializer

before_action :authenticate_api_user!, except: [:index, :show, :categories]
before_action :set_blog_post, only: %i[show update destroy]
after_action :set_cache_control, only: :index

def index
Expand All @@ -19,7 +18,7 @@ def index
end

def show
render json: @blog_post,
render json: blog_post,
serializer: BlogPostSerializer,
status: 200
end
Expand All @@ -42,25 +41,25 @@ def create

def update
if blog_post_params[:category].present?
@blog_post.categories.delete_all
@blog_post.category_list.add(blog_post_params[:category])
blog_post.categories.delete_all
blog_post.category_list.add(blog_post_params[:category])
end
if @blog_post.update(blog_post_params)
@blog_post.reload
render json: @blog_post,
if blog_post.update(blog_post_params)
blog_post.reload
render json: blog_post,
serializer: BlogPostSerializer,
status: 200
else
render json: ErrorSerializer.serialize(@blog_post.errors),
render json: ErrorSerializer.serialize(blog_post.errors),
status: :unprocessable_entity
end
end

def destroy
if @blog_post.destroy
if blog_post.destroy
render json: {}, status: :ok
else
render json: ErrorSerializer.serialize(@blog_post.errors),
render json: ErrorSerializer.serialize(blog_post.errors),
status: :unprocessable_entity
end
end
Expand All @@ -74,6 +73,10 @@ def categories

private

def blog_post
@blog_post ||= BlogPost.find(params[:id])
end

def blog_post_params
params.require(:blog_post).permit(
:title,
Expand All @@ -82,7 +85,6 @@ def blog_post_params
:category,
:is_published,
:organization_id,
{ images: [] },
blog_post_attachments_attributes: [
:id,
:file_type,
Expand All @@ -94,12 +96,9 @@ def blog_post_params
)
end

def set_blog_post
@blog_post = BlogPost.find(params[:id])
end

def filter_posts
blog_post = BlogPost.includes(:blog_post_attachments).includes(:categories).includes(:organization).includes(:user)
blog_post = blog_post.from_published_orgs unless params[:ignore_org_publish].present?
if params[:filter].present?
blog_post = blog_post.tagged_with(params[:filter][:category]) if params[:filter][:category].present?
blog_post = blog_post.where(is_published: params[:filter][:draft].downcase == 'false' ? true : false) if params[:filter][:draft].present?
Expand Down
1 change: 1 addition & 0 deletions app/controllers/api/v1/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def event

def fetch_events
events = Event.includes(:organization).includes(:user)
events = events.from_published_orgs unless params[:ignore_org_publish].present?
events = events.events_in_month(DateTime.strptime(params[:month], '%m')) if params[:month].present?
events = events.where(is_featured: true) if params[:featured].present?
events = events.where('starting_at >= ?', DateTime.parse(params[:starting_after])) if params[:starting_after].present?
Expand Down
30 changes: 30 additions & 0 deletions app/controllers/api/v1/org_profile_images_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Api
module V1
class OrgProfileImagesController < Api::V1::BaseController
include ErrorSerializer

def create
org_id = org_profile_image_params[:organization_id]
image = org_profile_image_params[:image]
@profile_image = OrgProfileImage.find_or_initialize_by(organization_id:org_id)
@profile_image.image = image
if @profile_image.save
render json: {url:@profile_image.image.url}, status: 200
else
render json: ErrorSerializer.serialize(@profile_image.errors),
status: :unprocessable_entity
end
end

private

def org_profile_image_params
params.permit(
:local_identifier,
:image,
:organization_id
)
end
end
end
end
2 changes: 1 addition & 1 deletion app/models/blog_post.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class BlogPost < ActiveRecord::Base
include OrgRecord
attr_accessible :title, :body, :posted_at, :user_id,
:is_published, :blog_post_attachments_attributes, :organization_id,
:category_list

acts_as_taggable_on :categories

belongs_to :user
Expand Down
6 changes: 6 additions & 0 deletions app/models/concerns/org_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module OrgRecord
extend ActiveSupport::Concern
included do
scope :from_published_orgs, -> { joins(:organization).where('organizations.is_published = true') }
end
end
2 changes: 1 addition & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class Event < ActiveRecord::Base
include OrgRecord
MAX_EVENTS_FEATURED_MONTH = 3
attr_accessible :title, :posted_at, :starting_at, :ending_at, :street_1,
:street_2, :city, :state_abbr, :zip, :phone, :external_url,
:organization_id, :is_featured, :body, :user_id, :is_all_day

belongs_to :user
belongs_to :organization

Expand Down
4 changes: 4 additions & 0 deletions app/models/org_profile_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class OrgProfileImage < ActiveRecord::Base
attr_accessible :local_identifier, :remote_url, :organization_id, :image
mount_uploader :image, OrgProfileImageUploader
end
48 changes: 48 additions & 0 deletions app/uploaders/org_profile_image_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class OrgProfileImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick

# Choose what kind of storage to use for this uploader:
# storage :file
storage :fog

# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
# "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
"org-profile-images"
end

# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url(*args)
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name , "default.png"].compact.join('_')
# end

# Process files as they are uploaded:
# process scale: [200, 300]
#
# def scale(width, height)
# # do something
# end

# Create different versions of your uploaded files:
# version :thumb do
# process resize_to_fit: [50, 50]
# end

# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_whitelist
# %w(jpg jpeg gif png)
# end

# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
def filename
SecureRandom.uuid
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
end

resources :blog_post_images
resources :org_profile_images

put 'services/:service_id/categories',
to: 'services#update_categories', as: :service_categories
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20190501022752_add_image_to_organization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddImageToOrganization < ActiveRecord::Migration
def change
add_column :organizations, :image, :string
end
end
11 changes: 11 additions & 0 deletions db/migrate/20190501144552_create_org_profile_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateOrgProfileImage < ActiveRecord::Migration
def change
create_table :org_profile_images do |t|
t.string :local_identifier
t.string :remote_url
t.integer :organization_id
t.index :organization_id
t.string :image
end
end
end
62 changes: 61 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,39 @@ CREATE SEQUENCE public.mail_addresses_id_seq
ALTER SEQUENCE public.mail_addresses_id_seq OWNED BY public.mail_addresses.id;


--
-- Name: org_profile_images; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.org_profile_images (
id integer NOT NULL,
local_identifier character varying,
remote_url character varying,
organization_id integer,
image character varying
);


--
-- Name: org_profile_images_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.org_profile_images_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- Name: org_profile_images_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.org_profile_images_id_seq OWNED BY public.org_profile_images.id;


--
-- Name: organizations; Type: TABLE; Schema: public; Owner: -
--
Expand Down Expand Up @@ -699,7 +732,8 @@ CREATE TABLE public.organizations (
rank integer,
approval_status public.post_approval_statuses,
is_published boolean DEFAULT false,
user_id integer
user_id integer,
image character varying
);


Expand Down Expand Up @@ -1130,6 +1164,13 @@ ALTER TABLE ONLY public.locations ALTER COLUMN id SET DEFAULT nextval('public.lo
ALTER TABLE ONLY public.mail_addresses ALTER COLUMN id SET DEFAULT nextval('public.mail_addresses_id_seq'::regclass);


--
-- Name: org_profile_images id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.org_profile_images ALTER COLUMN id SET DEFAULT nextval('public.org_profile_images_id_seq'::regclass);


--
-- Name: organizations id; Type: DEFAULT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -1305,6 +1346,14 @@ ALTER TABLE ONLY public.mail_addresses
ADD CONSTRAINT mail_addresses_pkey PRIMARY KEY (id);


--
-- Name: org_profile_images org_profile_images_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.org_profile_images
ADD CONSTRAINT org_profile_images_pkey PRIMARY KEY (id);


--
-- Name: organizations organizations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -1573,6 +1622,13 @@ CREATE INDEX index_locations_on_tsv_body ON public.locations USING gin (tsv_body
CREATE INDEX index_mail_addresses_on_location_id ON public.mail_addresses USING btree (location_id);


--
-- Name: index_org_profile_images_on_organization_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_org_profile_images_on_organization_id ON public.org_profile_images USING btree (organization_id);


--
-- Name: index_organizations_on_slug; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -2008,3 +2064,7 @@ INSERT INTO schema_migrations (version) VALUES ('20190426030916');

INSERT INTO schema_migrations (version) VALUES ('20190426032218');

INSERT INTO schema_migrations (version) VALUES ('20190501022752');

INSERT INTO schema_migrations (version) VALUES ('20190501144552');

0 comments on commit 03183e1

Please sign in to comment.