<commit_before>require 'support/exceptions'

class BaseController < JSONAPI::ResourceController
  include JSONAPI::Utils
  protect_from_forgery with: :null_session
  rescue_from ActiveRecord::RecordNotFound, with: :jsonapi_render_not_found
end

class PostsController < BaseController
  before_action :load_user, except: %i(create index_with_hash show_with_hash)

  # GET /users/:user_id/posts
  def index
    jsonapi_render json: @user.posts, options: { count: 100 }
  end

  # GET /index_with_hash
  def index_with_hash
    @posts = { data: [
      { id: 1, title: 'Lorem Ipsum' },
      { id: 2, title: 'Dolor Sit' }
    ]}
    # Example of response rendering from Hash + options:
    jsonapi_render json: @posts, options: { model: Post }
  end

  # GET /users/:user_id/posts/:id
  def show
    jsonapi_render json: @user.posts.find(params[:id])
  end

  # GET /show_with_hash/:id
  def show_with_hash
    # Example of response rendering from Hash + options: (2)
    jsonapi_render json: { data: { id: params[:id], title: 'Lorem ipsum' } },
                   options: { model: Post, resource: ::V2::PostResource }
  end

  # POST /posts
  def create
    post = Post.new(post_params)
    if post.save
      jsonapi_render json: post, status: :created
    else
      # Example of error rendering for Array of Hashes:
      errors = [{ id: 'title', title: 'Title can\'t be blank', code: '100' }]
      jsonapi_render_errors json: errors, status: :unprocessable_entity
    end
  end

  private

  def post_params
    resource_params.merge(user_id: relationship_params[:author])
  end

  def load_user
    @user = User.find(params[:user_id])
  end
end

class UsersController < BaseController
  # GET /users
  def index
    users = User.all
    jsonapi_render json: users
  end

  # GET /users/:id
  def show
    user = User.find(params[:id])
    jsonapi_render json: user
  end

  # POST /users
  def create
    user = User.new(resource_params)
    if user.save
      jsonapi_render json: user, status: :created
    else
      # Example of error rendering for ActiveRecord objects:
      jsonapi_render_errors json: user, status: :unprocessable_entity
    end
  end

  # PATCH /users/:id
  def update
    user = User.find(params[:id])
    if user.update(resource_params)
      jsonapi_render json: user
    else
      # Example of error rendering for exceptions or any object
      # that implements the "errors" method.
      jsonapi_render_errors ::Exceptions::MyCustomError.new(user)
    end
  end
end<commit_msg>Fix conflicts<commit_after>require 'support/exceptions'

class BaseController < JSONAPI::ResourceController
  include JSONAPI::Utils
  protect_from_forgery with: :null_session
  rescue_from ActiveRecord::RecordNotFound, with: :jsonapi_render_not_found
end

class PostsController < BaseController
  before_action :load_user, except: %i(create index_with_hash show_with_hash)

  # GET /users/:user_id/posts
  def index
    jsonapi_render json: @user.posts, options: { count: 100 }
  end

  # GET /index_with_hash
  def index_with_hash
    @posts = { data: [
      { id: 1, title: 'Lorem Ipsum' },
      { id: 2, title: 'Dolor Sit' }
    ]}
    # Example of response rendering from Hash + options:
    jsonapi_render json: @posts, options: { model: Post }
  end

  # GET /users/:user_id/posts/:id
  def show
    jsonapi_render json: @user.posts.find(params[:id])
  end

  # GET /show_with_hash/:id
  def show_with_hash
    # Example of response rendering from Hash + options: (2)
    jsonapi_render json: { data: { id: params[:id], title: 'Lorem ipsum' } },
                   options: { model: Post, resource: ::V2::PostResource }
  end

  # POST /posts
  def create
    post = Post.new(post_params)
    if post.save
      jsonapi_render json: post, status: :created
    else
      # Example of error rendering for Array of Hashes:
      errors = [{ id: 'title', title: 'Title can\'t be blank', code: '100' }]
      jsonapi_render_errors json: errors, status: :unprocessable_entity
    end
  end

  private

  def post_params
    resource_params.merge(user_id: relationship_params[:author])
  end

  def load_user
    @user = User.find(params[:user_id])
  end
end

class UsersController < BaseController
  # GET /users
  def index
    users = User.all
    jsonapi_render json: users
  end

  # GET /users/:id
  def show
    user = User.find(params[:id])
    jsonapi_render json: user
  end

  # POST /users
  def create
    user = User.new(resource_params)
    if user.save
      jsonapi_render json: user, status: :created
    else
      # Example of error rendering for ActiveRecord objects:
      jsonapi_render_errors json: user, status: :unprocessable_entity
    end
  end

  # PATCH /users/:id
  def update
    user = User.find(params[:id])
    if user.update(resource_params)
      jsonapi_render json: user
    else
      # Example of error rendering for exceptions or any object
      # that implements the "errors" method.
      jsonapi_render_errors ::Exceptions::MyCustomError.new(user)
    end
  end

  private

  def user_params
    params.require(:data).require(:attributes).permit(:first_name, :last_name, :admin)
  end
end
{'fraction_non_alphanumeric': 0.09396233741021161, 'fraction_numerical': 0.0038827412152980002, 'mean_word_length': 3.216039279869067, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7528905', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1626, 'n_words': 471}