This is the product model in my gems lib s4s models folder:
module S4s
module Models
class Product < ActiveRecord::Base
self.table_name = 'product'
has_many :images, foreign_key: :product_id, class_name: 'S4s::Models::ProductImage'
has_many :primary_images, -> { where(primary_image: true) }, foreign_key: :product_id, class_name: 'S4s::Models::ProductImage'
has_one :image, foreign_key: :product_id, class_name: 'S4s::Models::ProductImage'
has_many :product_images, foreign_key: :product_id, class_name: 'S4s::Models::ProductImage'
end
end
end
This is the product_image.rb file in my gems lib s4s models folder:
require 'paperclip'
module S4s
module Models
class ProductImage < ActiveRecord::Base
self.table_name = 'product_images'.freeze
include S4s::Models::Concerns::Upload
TYPE_HIGH_RESOLUTION = 'highResolution'
TYPE_ADDITIONAL = 'additional'
IMAGE_VERSIONS = %w|mini small medium xs sm fullxs fullsm large|
attr_accessor :image_file_size
alias_attribute :image_file_name, :original_file_name
alias_attribute :image_content_type, :file_ext
alias_attribute :image_updated_at, :updated_at
belongs_to :product, foreign_key: 'product_id'.freeze, class_name: 'S4s::Models::Product'.freeze
belongs_to :color, foreign_key: 'color_id'.freeze, class_name: 'S4s::Models::Dictionary::Color'.freeze
validates :title, presence: true
scope :additional, -> { where(image_type: TYPE_ADDITIONAL) }
scope :high_resolution, -> { where(image_type: TYPE_HIGH_RESOLUTION) }
scope :primary_images, -> { where(primary_image: true) }
after_initialize :set_default_value
after_save :set_product_colors!
add_attachment :image,
styles: {
mini: ['100x100#', :jpg],
small: ['220x220#', :jpg],
medium: ['380x380#', :jpg],
xs: ['240x240', :jpg],
sm: ['348x348#', :jpg],
fullxs: ['480x480#', :jpg],
fullsm: ['768x768#', :jpg],
large: ['1000x1000', :jpg],
},
path_block: -> (style) { self.build_path(style) },
matches: /(png|jpe?g|gif)/i
# Populate file_name attribute with the current title
before_image_post_process :set_file_name!
public
def url(type = 'mini')
return nil unless self.product_id.present?
image.url(type)
end
def urls
Hash[IMAGE_VERSIONS.map { |v| [v, self.url(v)] }]
end
def as_json(opts = {})
{
id: self.id,
is_primary_image: primary_image?,
product_id: self.product_id,
title: self.title,
color: 'n/a',
sku: self.sku,
position: self.position,
image_type: self.image_type,
urls: self.urls
}
end
def build_path(style)
return nil if product.nil?
build_asset_path(style, !(new_system? || title_used?))
end
private
def build_asset_path(style, old_format = false)
"/products/#{product_id}/#{build_slug(old_format)}-#{style}.#{_find_extension(image_content_type)}"
end
def build_slug(old_format)
if old_format && !file_name.present?
"#{product.name.parameterize}#{position > 0 ? "-#{position}" : ''}"
else
file_name
end
end
def set_product_colors!
_colors = self.product.images.map(&:color).compact
if self.product.colors.map(&:id).sort != _colors.map(&:id).sort
self.product.update_attribute :colors, _colors
end
end
def set_file_name!
self.file_name = SecureRandom.hex(20)
self.new_system = true
end
def set_default_value
self.position ||= 0
self.new_system = true if self.new_system.nil?
end
end
end
end
The Logic is that we are calling these models to different apps using S4s::Models::ModelName
Below is the controller file that I am using to render json(this controller is in another app):
class HalfsController < ApplicationController
def index
@hotspot = S4s::Models::Product.all
render json: @hotspot.to_json(include: :product_images)
end
...
end
I need a nested format of Product_image objects inside product object. I am New to ruby and rails framework, please help me out.
note: I have tried all format to_json such as :product_images and :product_image.. nested is working for many other models in gems but these are not working for product and product_images.. they have used paperclip to upload and generate images url