Ruby on Rails Gem for Assets Centralization Project (NEW CDN SERVER) at PT. Tri Saudara Sentosa Industri
ONGOING DEVELOPMENT
Add this following code in GemFile
gem 'centralized_assets', git: 'https://github.com/nandangpk/centralized_assets_gems'
then run bundle install
to install the gem
Run rails generate centralized_assets:install
, it will return response :
create config/initializers/centralized_assets.rb
sign that we just generate a new configuration file at :
config/initializers/centralized_assets.rb
CentralizedAssets.configure do |config|
config.database_url = "postgresql://username:password@localhost:5432/db_name"
config.server_url = "..."
config.token = "..."
end
config_name | detail |
---|---|
database_url | STILL BUG - FIXED ASAP |
server_url | assets server host |
token | application token for assets server |
app/models/example.rb
require 'centralized_assets' # add this line
class ApplicationRecord < ActiveRecord::Base
include CentralizedAssets # add this line
...
end
app/models/example.rb
class ExampleModel < ApplicationRecord
has_attachment :file # add this method
...
end
There's several way to configure the controller:
-
Permitted parameters (Recommended)
app/controllers/examples_controller.rb
... def create ... example = ExampleModel.new(example_params) example.save ... end def update ... example = ExampleModel.find(:params[:id]) example.update(example_params) ... end private def example_params params.require(:example).permit(.., :attachment, ..) end ...
if your controller configuration using
params.require.permit
, you can generate the HTML file form like this:app/views/examples/_form.html.erb
<%= form_with(model: example, multipart: true) do |form| %> ... <%= form.file_field :attachment %> ... <% end %>
-
Manual
app/controllers/examples_controller.rb
... def create ... example = ExampleModel.new ... example.attachment = params[:attachment] ... example.save ... end def update ... example = ExampleModel.find(:params[:id]) ... example.attachment = params[:attachment] ... example.save ... end ...
or you can write your controller like this:
app/controllers/examples_controller.rb
... def create ... ExampleModel.create( ... attachment: params[:attachment] ... ) ... end def update ... example = ExampleModel.find(:params[:id]) example.update( ... attachment: params[:attachment] ... ) ... end ...
if your controller configuration not using
params.require.permit
, you can generate the HTML file form like this:app/views/examples/_form.html.erb
<form ectype="multipart/form-data"> ... <input type="file" name="attachment"> ... </form>
example = ExampleModel.find(params[:id])
example.file
# if attachment is present
# return {:url => "DIRECT_URL_TO_ASSETS:string", :filename=>"ASSETS_FILENAME:string", :ext=>"ASSETS_EXTENSION:string", :size=>{:kb=>(SIZE_IN_KB:integer), :mb=>(SIZE_IN_MB:integer)}}
# else
# return nil
example.file.present?
# if attachment is present
# return true
# else
# return false
example.file[:hash_key]
# if hash_key is valid
# return hash data
# else
# raise "Invalid hash keys, accept only: [ACCEPTED_HASH_KEYS.to_sym]"
app/models/example.rb
class ExampleModel < ApplicationRecord
has_attachment :files # add this method
...
end
There's several way to configure the controller:
-
Permitted parameters (Recommended)
app/controllers/examples_controller.rb
... def create ... example = ExampleModel.new(example_params) example.save ... end def update ... example = ExampleModel.find(:params[:id]) example.update(example_params) ... end private def example_params params.require(:example).permit(.., attachment: [], ..) end ...
if your controller configuration using
params.require.permit
, you can generate the HTML file form like this:app/views/examples/_form.html.erb
<%= form_with(model: example, multipart: true) do |form| %> ... <%= form.file_field :attachment, :multiple => true %> ... <% end %>
-
Manual
app/controllers/examples_controller.rb
... def create ... example = ExampleModel.new ... example.attachment = params[:attachment] ... example.save ... end def update ... example = ExampleModel.find(:params[:id]) ... example.attachment = params[:attachment] ... example.save ... end ...
or you can write your controller like this:
app/controllers/examples_controller.rb
... def create ... ExampleModel.create( ... attachment: params[:attachment] ... ) ... end def update ... example = ExampleModel.find(:params[:id]) example.update( ... attachment: params[:attachment] ... ) ... end ...
if your controller configuration not using
params.require.permit
, you can generate the HTML file form like this:app/views/examples/_form.html.erb
<form ectype="multipart/form-data"> ... <input type="file" name="attachment[]" multiple> ... </form>
example = ExampleModel.find(params[:id])
example.files
# if attachment is present (atleast one)
# return [ {:url => "DIRECT_URL_TO_ASSETS:string", :filename=>"ASSETS_FILENAME:string", :ext=>"ASSETS_EXTENSION:string", :size=>{:kb=>(SIZE_IN_KB:integer), :mb=>(SIZE_IN_MB:integer)}}, .., .. ]
# else
# return nil
example.files.present?
# if attachment is present (atleast one)
# return true
# else
# return false
example.files.each do |file|
file
# return {:url => "DIRECT_URL_TO_ASSETS:string", :filename=>"ASSETS_FILENAME:string", :ext=>"ASSETS_EXTENSION:string", :size=>{:kb=>(SIZE_IN_KB:integer), :mb=>(SIZE_IN_MB:integer)}}
file[:hash_key]
# return hash data
end